Skip to content

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