Package: elementpath
The proposal of this package is to provide XPath 1.0, 2.0, 3.0 and 3.1
selectors for ElementTree XML data structures, both for the standard
ElementTree library and for thelxml.etreelibrary.Forlxml.etreethis package can be useful for providing
XPath 2.0/3.0/3.1 selectors, becauselxml.etreealready
has it’s own implementation of XPath 1.0.Installation and usageYou can install the package withpipin a Python 3.8+ environment:pip install elementpathFor using it import the package and apply the selectors on ElementTree nodes:>>> 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 ...>]TheselectAPI provides the standard XPath result format that is a list or an elementary
datatype’s value. If you want only to iterate over results you can use the generator functioniter_selectthat accepts the same arguments ofselect.The selectors API works also using XML data trees based on thelxml.etreelibrary:>>> import elementpath
>>> import lxml.etree as etree
>>> root = etree.XML('<A><B1/><B2><C1/><C2/><C3/></B2></A>')
>>> elementpath.select(root, '/A/B2/*')
[<Element C1 at ...>, <Element C2 at ...>, <Element C3 at ...>]When you need to apply the same XPath expression to several XML data you can also use theSelectorclass, creating an instance and then using it to apply the path on distinct XML
data:>>> import elementpath
>>> import lxml.etree as etree
>>> selector = elementpath.Selector('/A/*/*')
>>> root = etree.XML('<A><B1/><B2><C1/><C2/><C3/></B2></A>')
>>> selector.select(root)
[<Element C1 at ...>, <Element C2 at ...>, <Element C3 at ...>]
>>> root = etree.XML('<A><B1><C0/></B1><B2><C1/><C2/><C3/></B2></A>')
>>> selector.select(root)
[<Element C0 at ...>, <Element C1 at ...>, <Element C2 at ...>, <Element C3 at ...>]Public API classes and functions are described into theelementpath manual on the “Read the Docs” site.For default the XPath 2.0 is used. If you need XPath 1.0 parser provide theparserargument:>>> from elementpath import select, XPath1Parser
>>> from xml.etree import ElementTree
>>> root = ElementTree.XML('<A><B1/><B2><C1/><C2/><C3/></B2></A>')
>>> select(root, '/A/B2/*', parser=XPath1Parser)
[<Element 'C1' at ...>, <Element 'C2' at ...>, <Element 'C3' at ...>]For XPath 3.0/3.1 import the parser fromelementpath.xpath3subpackage, that is not loaded
for default:>>> from elementpath.xpath3 import XPath3Parser
>>> select(root, 'math:atan(1.0e0)', parser=XPath3Parser)
0.7853981633974483Note:XPath3Parseris an alias ofXPath31Parser.If you need only XPath 3.0 you can also use a more specific subpackage,
avoiding the loading of XPath 3.1 implementation:>>> from elementpath.xpath30 import XPath30Parser
>>> select(root, 'math:atan(1.0e0)', parser=XPath30Parser)
0.7853981633974483ContributingYou can contribute to this package reporting bugs, using the issue tracker or by a pull request.
In case you open an issue please try to provide a test or test data for reproducing the wrong
behaviour. The provided testing code shall be added to the tests of the package.The XPath parsers are based on an implementation of the Pratt’s Top Down Operator Precedence parser.
The implemented parser includes some lookup-ahead features, helpers for registering tokens and for
extending language implementations. Also the token class has been generalized using aMutableSequenceas base class. Seetdop.pyfor the basic internal classes andxpath1_parser.pyfor extensions
and for a basic usage of the parser.If you like you can use the basic parser and tokens provided by thetdop.pymodule to
implement other types of parsers (I think it could be also a funny exercise!).LicenseThis software is distributed under the terms of the MIT License.
See the file ‘LICENSE’ in the root directory of the present
distribution, orhttp://opensource.org/licenses/MIT.