Skip to content

Commit

Permalink
Fix document root path step (issue #46)
Browse files Browse the repository at this point in the history
  - Always select the document root even if item is a child
  • Loading branch information
brunato committed May 24, 2022
1 parent 737ef6c commit c3a34af
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 5 additions & 4 deletions elementpath/xpath1/_xpath1_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,10 +675,11 @@ def select_child_path(self, context=None):
if is_document_node(context.root):
yield context.root
elif len(self) == 1:
if is_document_node(context.root) or context.item is context.root:
if not isinstance(context, XPathSchemaContext):
context.item = None
yield from self[0].select(context)
if not isinstance(context, XPathSchemaContext):
context.item = None
else:
context.item = context.root
yield from self[0].select(context)
else:
items = set()
for _ in context.inner_focus_select(self[0]):
Expand Down
6 changes: 4 additions & 2 deletions tests/test_xpath1_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ def test_node_types(self):

context = XPathContext(root)
context.item = None
self.check_value("/self::node()", expected=[], context=context)
# lxml differs: doesn't consider the document position even if select from an ElementTree
self.check_value("/self::node()", expected=[root], context=context)

context.item = 1
self.check_value("self::node()", expected=[], context=context)

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

context = XPathContext(root, item=root[0][0])
self.check_value('/A', [], context=context)
self.check_value('/A', [root], context=context)

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

0 comments on commit c3a34af

Please sign in to comment.