Skip to content

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

Open
danifus wants to merge 3 commits intopython:mainfrom
danifus:b132637
Open

gh-132637: Fix positional predicates in xpath when a default namespace is provided#132822
danifus wants to merge 3 commits intopython:mainfrom
danifus:b132637

Conversation

@danifus
Copy link
Copy Markdown
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.

@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant