Skip to main content

An XPath 3.1 Parser

Project description

Parse XPATH 3.1 using Pyparsing

XPath (XML Path Language) is a query language for selecting nodes from an XML document. In addition, XPath is used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML document. XPath is maintained by the World Wide Web Consortium (W3C).

Pyparsing is a parsing module used to construct grammar in Python. XPyth-parser uses Pyparsing to parse XPath strings, and offers an additional abstraction layer.

Status

This library is an attempt to create a parser which can be used both to query XML documents, as well as calculation tasks. The original plan was to support both options. However, XPath 3.1 is not widely used, so use cases are sparse. Parsing XPath 3.1 on a grammar level should still be supported, but not all information may be available when using the abstraction layer. Most importantly, there will be XPath functions missing.

Dealing with dynamic contexts (i.e., parsing XML as Parser.xml will be done using LXML for now). In a way, XPyth-parser is at the present moment a fancy wrapper around LXML, in order to support some XPath 2.0+ functionality.

Alternatives

For most use cases, there will be (better) alternatives to this project. LXML is Pythonic binding for the C libraries libxml2 and libxslt. If only XPath 1.0 is needed, LXML will be a better solution.

Requirements

xpyth-parser depends on LXML, PyParsing. For parsing dates we use Isodate.

Goals

This project started out with a specific goal: to parse XBRL formula tests. These tests are heavily reliant on XBRL specific XPath 2.0 functions. Because of this, the author of this library is focussing on correctly interpreting these functions.

Examples

from xpyth_parser.parse import Parser 
count = Parser("count(1,2,3)").run()
print(count) -> 3

This will give a wrapper class which contains the resolved syntax tree in count.XPath and the answer in count.resolved_answer

Parsing only

It is also possible to only parse the string, but not try to resolve the static and dynamic context

count = Parser("count(1,2,3), no_resolve=True")

count.xpath will be the full syntax tree, instead of having functions processed and contexts applied. count.run() will resolve the expression as if no_resolve=False. contexts might need to be passed to the object beforehand.

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

xpyth_parser-0.0.10.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xpyth_parser-0.0.10-py3-none-any.whl (25.9 kB view details)

Uploaded Python 3

File details

Details for the file xpyth_parser-0.0.10.tar.gz.

File metadata

  • Download URL: xpyth_parser-0.0.10.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for xpyth_parser-0.0.10.tar.gz
Algorithm Hash digest
SHA256 797f1680a9a7dc9fc49cba8577652f5cdd9f1f735f33a684f70f4e1b303abc39
MD5 cdc4f4655d5c1f6285d99f712e250be1
BLAKE2b-256 bbcdb9a769a1b37ad85750ed097b909818bfe838d83fb7f64c922f9af206a12b

See more details on using hashes here.

File details

Details for the file xpyth_parser-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: xpyth_parser-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 25.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for xpyth_parser-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 51cf8fd307ae42af2b3b3a19acfb05a6da8cf90ede8f303f6d8196c154d511b0
MD5 3e67d986828c2107a0dfc25492949d71
BLAKE2b-256 fc6cd04d5776c310803298e6594460823e98d263f059b23b0de755de76f38a3d

See more details on using hashes here.

Supported by

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