Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select fails when XML contains leading comment #74

Closed
sean-moore3 opened this issue Aug 11, 2024 · 2 comments
Closed

Select fails when XML contains leading comment #74

sean-moore3 opened this issue Aug 11, 2024 · 2 comments

Comments

@sean-moore3
Copy link

Hi,

I stumbled upon this issue while upgrading from 4.1.5. I can reproduce in 4.2.0 and later.

import lxml.etree
import elementpath


root = lxml.etree.XML("<!--comment--><root><trunk><branch></branch></trunk></root>")
trunk = elementpath.select(root, "trunk")
print(trunk)
"""[]"""
root = lxml.etree.XML("<root><trunk><branch></branch></trunk></root>")
trunk = elementpath.select(root, "trunk")
print(trunk)
"""[<Element trunk at 0x102b862c0>]"""
@brunato
Copy link
Member

brunato commented Aug 14, 2024

Hi, with v4.2.0 something is changed with node tree build, see:

https://elementpath.readthedocs.io/en/latest/advanced.html#the-context-root-and-the-context-item

The change has been necessary to handle XML document and fragments. A root node without siblings can skip the document position if not explicitly selected by the XPath expression (e.g. /root/trunk). A comment sibling of the root element can't be ignored so the initial position is set to the document.

The keyword arguments item and fragment can be used to set the initial node or to skip the dummy document creation.

@sean-moore3
Copy link
Author

Thanks! I found fragment to be a good fit for my use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants