Skip to main content

XPath 1.0/2.0/3.0/3.1 parsers and selectors for ElementTree and lxml

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

elementpath-4.1.4.tar.gz (321.6 kB view details)

Uploaded Source

Built Distribution

elementpath-4.1.4-py3-none-any.whl (216.4 kB view details)

Uploaded Python 3

File details

Details for the file elementpath-4.1.4.tar.gz.

File metadata

  • Download URL: elementpath-4.1.4.tar.gz
  • Upload date:
  • Size: 321.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for elementpath-4.1.4.tar.gz
Algorithm Hash digest
SHA256 f991c42ff66fa06e219141ccf65890261e6635b448e7d4c2d8b62dc5bf1de9e8
MD5 81f9dc2c1e22b96e6f479ead985a16b8
BLAKE2b-256 01f530643fa6a71a9b3565248ca0a303d1d9ae4a323683616c1c457186429a87

See more details on using hashes here.

File details

Details for the file elementpath-4.1.4-py3-none-any.whl.

File metadata

  • Download URL: elementpath-4.1.4-py3-none-any.whl
  • Upload date:
  • Size: 216.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for elementpath-4.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e7c6d25546dfb381a2c9cde3b78c0c40f52811e06eb810faf019e16c531a74bf
MD5 0f571d0b1fd613b4990380c8dbe4a628
BLAKE2b-256 392695706ebdf6944a5d011a8615abd9d11e0ba73ca42f0fa7b66e7df31abe55

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page