elementpath 5.0.4


pip install elementpath

  Latest version

Released: Aug 16, 2025

Project Links

Meta
Author: Davide Brunato
Requires Python: >=3.9

Classifiers

Development Status
  • 5 - Production/Stable

Intended Audience
  • Developers
  • Information Technology
  • Science/Research

Operating System
  • OS Independent

Programming Language
  • Python
  • Python :: 3
  • Python :: 3 :: Only
  • Python :: 3.9
  • Python :: 3.10
  • Python :: 3.11
  • Python :: 3.12
  • Python :: 3.13
  • Python :: 3.14
  • Python :: Implementation :: CPython
  • Python :: Implementation :: PyPy

Topic
  • Software Development :: Libraries
  • Text Processing :: Markup :: XML
https://img.shields.io/pypi/v/elementpath.svg https://img.shields.io/pypi/pyversions/elementpath.svg https://img.shields.io/pypi/implementation/elementpath.svg MIT License https://img.shields.io/pypi/dm/elementpath.svg

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 the lxml.etree library.

For lxml.etree this package can be useful for providing XPath 2.0/3.0/3.1 selectors, because lxml.etree already has it’s own implementation of XPath 1.0.

Installation and usage

You can install the package with pip in a Python 3.9+ environment:

pip install elementpath

For 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 ...>]

The select API 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 function iter_select that accepts the same arguments of select.

The selectors API works also using XML data trees based on the lxml.etree library:

>>> 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 the Selector class, 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 the elementpath manual on the “Read the Docs” site.

For default the XPath 2.0 is used. If you need XPath 1.0 parser provide the parser argument:

>>> 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 from elementpath.xpath3 subpackage, that is not loaded for default:

>>> from elementpath.xpath3 import XPath3Parser
>>> select(root, 'math:atan(1.0e0)', parser=XPath3Parser)
0.7853981633974483

Note: XPath3Parser is an alias of XPath31Parser.

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.7853981633974483

Contributing

You 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 a MutableSequence as base class. See tdop.py for the basic internal classes and xpath1_parser.py for extensions and for a basic usage of the parser.

If you like you can use the basic parser and tokens provided by the tdop.py module to implement other types of parsers (I think it could be also a funny exercise!).

License

This software is distributed under the terms of the MIT License. See the file ‘LICENSE’ in the root directory of the present distribution, or http://opensource.org/licenses/MIT.

5.0.4 Aug 16, 2025
5.0.3 Jun 28, 2025
5.0.2 Jun 18, 2025
5.0.1 May 11, 2025
5.0.0 Apr 27, 2025
4.8.0 Mar 03, 2025
4.7.0 Dec 20, 2024
4.6.0 Oct 27, 2024
4.5.0 Sep 09, 2024
4.4.0 Mar 11, 2024
4.3.0 Feb 17, 2024
4.2.1 Feb 10, 2024
4.2.0 Feb 03, 2024
4.1.5 Jul 25, 2023
4.1.4 Jun 26, 2023
4.1.3 Jun 17, 2023
4.1.2 May 04, 2023
4.1.1 Apr 11, 2023
4.1.0 Mar 21, 2023
4.0.1 Feb 02, 2023
4.0.0 Feb 01, 2023
3.0.2 Aug 12, 2022
3.0.1 Jul 23, 2022
3.0.0 Jul 16, 2022
2.5.3 May 30, 2022
2.5.2 May 17, 2022
2.5.1 Apr 28, 2022
2.5.0 Mar 04, 2022
2.4.0 Nov 09, 2021
2.3.2 Sep 16, 2021
2.3.1 Sep 07, 2021
2.3.0 Sep 01, 2021
2.2.3 Jun 17, 2021
2.2.2 May 03, 2021
2.2.1 Mar 24, 2021
2.2.0 Mar 01, 2021
2.1.4 Feb 10, 2021
2.1.3 Jan 30, 2021
2.1.2 Jan 23, 2021
2.1.1 Jan 06, 2021
2.1.0 Jan 05, 2021
2.0.5 Dec 02, 2020
2.0.4 Oct 30, 2020
2.0.3 Sep 13, 2020
2.0.2 Sep 03, 2020
2.0.1 Aug 24, 2020
2.0.0 Aug 14, 2020
1.4.6 Jun 15, 2020
1.4.5 May 22, 2020
1.4.4 Apr 23, 2020
1.4.3 Mar 19, 2020
1.4.2 Mar 13, 2020
1.4.1 Jan 28, 2020
1.4.0 Dec 31, 2019
1.3.3 Dec 17, 2019
1.3.2 Dec 10, 2019
1.3.1 Oct 21, 2019
1.3.0 Oct 11, 2019
1.2.2 Oct 21, 2019
1.2.1 Aug 30, 2019
1.2.0 Aug 14, 2019
1.1.8 May 20, 2019
1.1.7 Apr 25, 2019
1.1.6 Mar 28, 2019
1.1.5 Feb 23, 2019
1.1.4 Feb 22, 2019
1.1.3 Feb 06, 2019
1.1.2 Jan 30, 2019
1.1.1 Jan 19, 2019
1.1.0 Dec 23, 2018
1.0.12 Sep 01, 2018
1.0.11 Jul 25, 2018
1.0.10 Jun 15, 2018
1.0.8 Jun 13, 2018
1.0.7 May 07, 2018
1.0.6 May 02, 2018
1.0.5 Mar 31, 2018
1.0.4 Mar 27, 2018
1.0.3 Mar 27, 2018
1.0.2 Mar 27, 2018
1.0.1 Mar 27, 2018
1.0 Mar 26, 2018

Wheel compatibility matrix

Platform Python 3
any

Files in release

Extras:
Dependencies: