Reputation: 10156
Is it possible to use XPath 2.0 functions like starts-with()
, ends-with()
and contains()
in Python? I was trying to use lxml
and defusedxml
, but unfortunately they do not support any of these functions.
I know I can use substring()
or matches()
for workaround, but I have really complicated case, so it would be nicer to deal with more readable functions.
Any lib that supports XPath 2.0 spec?
Upvotes: 1
Views: 1294
Reputation: 627
It's probably easiest to use elementpath for this, which supports the built-in xml.etree module and lxml.
>>> import elementpath
>>> from xml.etree import ElementTree
>>> root = ElementTree.XML('<A><B1/><B2><C1/><C2/><C3/></B2></A>')
>>> elementpath.select(root, '/A/B2/*')
[<Element 'C1' at ...>, <Element 'C2' at ...>, <Element 'C3' at ...>]
Upvotes: 2
Reputation: 771
The python wheels for SaxonC 12.X is the official release, which you can find here: https://pypi.org/user/saxonica/
Upvotes: 1
Reputation: 522
saxonpy is based on Saxon He and supports Xpath 2.0. You can check the github repo for some examples to get you started:
https://github.com/tennom/saxonpy
Upvotes: 1