Skip to main content

XPath 1.0/2.0 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://travis-ci.org/sissaschool/elementpath.svg?branch=master https://img.shields.io/pypi/dm/elementpath.svg https://img.shields.io/badge/Maintained%3F-yes-green.svg

The proposal of this package is to provide XPath 1.0 and 2.0 selectors for Python’s 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 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.5+ 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.

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_parser.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_parser.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-2.0.1.tar.gz (186.1 kB view details)

Uploaded Source

Built Distribution

elementpath-2.0.1-py3-none-any.whl (121.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: elementpath-2.0.1.tar.gz
  • Upload date:
  • Size: 186.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.5

File hashes

Hashes for elementpath-2.0.1.tar.gz
Algorithm Hash digest
SHA256 be296a00a8ebbb858864c577b4c8cc8e2a72d8e6a94c58d725a2b64a1c6aa72f
MD5 9041958cdf9af0a5e503ee258ee6967c
BLAKE2b-256 57bf157a800b5601dc583a10ccde5446a953192f92debaaee016d214c114dffc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: elementpath-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 121.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.5

File hashes

Hashes for elementpath-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2c8207772c4beb859cfbf9a6ac866349ce5d2ea49d94d4f6132f66fecbea7fd4
MD5 44686dec8113aa3062f5a322238697ad
BLAKE2b-256 f4da40c7ea04e6242f38de608f9789a28c86ab6a367f565f9bc4e4e0a6a52a37

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