Nickon
Nickon

Reputation: 10156

Library that supports XPath 2.0 in Python

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

Answers (3)

Ryan Patterson
Ryan Patterson

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

ond1
ond1

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

Mihail-Cosmin Munteanu
Mihail-Cosmin Munteanu

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

Related Questions