XPath 1.0/2.0 parsers and selectors for ElementTree.
Project description
===========
elementpath
===========
.. elementpath-introduction
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 <http://lxml.de>`_ library.
For `lxml.etree <http://lxml.de>`_ this package can be useful for providing XPath 2.0 selectors,
because `lxml.etree <http://lxml.de>`_ already has it's own implementation of XPath 1.0.
The XPath 2.0 functions implementation not completed yet, due to wide number of functions that this
language provides. If you want you can contribute to add an unimplemented function see the section below.
Installation and usage
----------------------
You can install the package with *pip* in a Python 2.7 or Python 3.3+ environment::
pip install elementpath
For using it import the package and apply the selectors on ElementTree nodes:
.. doctest::
>>> 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 <http://lxml.de>`_
library:
.. doctest::
>>> 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:
.. doctest::
>>> 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 <http://elementpath.readthedocs.io/en/latest/>`_.
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.
elementpath
===========
.. elementpath-introduction
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 <http://lxml.de>`_ library.
For `lxml.etree <http://lxml.de>`_ this package can be useful for providing XPath 2.0 selectors,
because `lxml.etree <http://lxml.de>`_ already has it's own implementation of XPath 1.0.
The XPath 2.0 functions implementation not completed yet, due to wide number of functions that this
language provides. If you want you can contribute to add an unimplemented function see the section below.
Installation and usage
----------------------
You can install the package with *pip* in a Python 2.7 or Python 3.3+ environment::
pip install elementpath
For using it import the package and apply the selectors on ElementTree nodes:
.. doctest::
>>> 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 <http://lxml.de>`_
library:
.. doctest::
>>> 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:
.. doctest::
>>> 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 <http://elementpath.readthedocs.io/en/latest/>`_.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
elementpath-1.0.7.tar.gz
(31.7 kB
view details)
Built Distribution
File details
Details for the file elementpath-1.0.7.tar.gz
.
File metadata
- Download URL: elementpath-1.0.7.tar.gz
- Upload date:
- Size: 31.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b0fc60097b21b24c023060a7f7d21ff0941618745d9ee088c57effe8ff939af |
|
MD5 | 7417145019200ddf91a59e9cc7eb3425 |
|
BLAKE2b-256 | eae28eafa2c6094e7b83cc8e77681df3e1e84bf765a85564e7cb969bad42abd9 |
File details
Details for the file elementpath-1.0.7-py2.py3-none-any.whl
.
File metadata
- Download URL: elementpath-1.0.7-py2.py3-none-any.whl
- Upload date:
- Size: 43.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 652fb89d2939cd815810cb49560c4509b8462a5636dfa14604d12c40f73c0991 |
|
MD5 | b03cbe560ad18d30f338bb748455bed3 |
|
BLAKE2b-256 | fed9350ddd869a604f06a8ecd0c8a27e5fcbcd9979f50a305f4583c866be4033 |