Skip to content

Commit c3a34af

Browse files
committed
Fix document root path step (issue #46)
- Always select the document root even if item is a child
1 parent 737ef6c commit c3a34af

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

elementpath/xpath1/_xpath1_operators.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,10 +675,11 @@ def select_child_path(self, context=None):
675675
if is_document_node(context.root):
676676
yield context.root
677677
elif len(self) == 1:
678-
if is_document_node(context.root) or context.item is context.root:
679-
if not isinstance(context, XPathSchemaContext):
680-
context.item = None
681-
yield from self[0].select(context)
678+
if not isinstance(context, XPathSchemaContext):
679+
context.item = None
680+
else:
681+
context.item = context.root
682+
yield from self[0].select(context)
682683
else:
683684
items = set()
684685
for _ in context.inner_focus_select(self[0]):

tests/test_xpath1_parser.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,9 @@ def test_node_types(self):
451451

452452
context = XPathContext(root)
453453
context.item = None
454-
self.check_value("/self::node()", expected=[], context=context)
454+
# lxml differs: doesn't consider the document position even if select from an ElementTree
455+
self.check_value("/self::node()", expected=[root], context=context)
456+
455457
context.item = 1
456458
self.check_value("self::node()", expected=[], context=context)
457459

@@ -1254,7 +1256,7 @@ def test_path_step_operator(self):
12541256
self.check_value('/A', [root], context=context)
12551257

12561258
context = XPathContext(root, item=root[0][0])
1257-
self.check_value('/A', [], context=context)
1259+
self.check_value('/A', [root], context=context)
12581260

12591261
def test_path_step_operator_with_duplicates(self):
12601262
root = self.etree.XML('<A>10<B a="2">11</B>10<B a="2"/>10<B>11</B></A>')

0 commit comments

Comments
 (0)