Skip to content

gh-132637: Fix positional predicates in xpath when a default namespace is provided #132822

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

danifus
Copy link
Contributor

@danifus danifus commented Apr 23, 2025

xpaths with a positional predicate (.//b[1]) would fail when the namespaces argument of find() or findall() supplied a default namespace.

This was due to xpath_tokenizer() erroneously prepending the default namespace to these positional predicates. .//b[1] would have a positional predicate of {the-supplied-default-namespace}1 and .//b[last()] would be {the-supplied-default-namespace}last(). The integer case would cause the xpath not to match the intended elements and the last() predicate would raise an exception as the xpath function {the-supplied-default-namespace}last() is not supported (because it doesn't exist).

This PR fixes the issue by altering the regex that parses the xpath expression to pick out last() explicitly as that is the only function that is supported in Python's imlementation and we only know that it is a function, rather than a tag name, when we reach () so it was easier to grab last and () together which were previously parsed separately (the generator still returns them separately for compatibility).

We also test if the tag is a number possibly preceded by a - or + and exclude prepending the namespace in those cases.

Ultimately a comment in the code suggests the proper fix:

# FIXME: replace with real parser!!! refs:
# http://javascript.crockford.com/tdop/tdop.html

but despite the known preferences of some contributors, replacing this regex parser with a real parser seems unlikely at this time.

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

Successfully merging this pull request may close these issues.

1 participant