Skip to main content

Manipulate xpath expressions in pure python

Project description

ci pypi

This is a small, lightweight Python library to aide in the manipulations of xpath expressions, allowing you to manipulate them as Python objects with Python expressions. For example:

from xpath import Expression as E, Attribute as A

root_node = E('/root')
all_children = root_node.children         # /root/*
root_node_name =           # name(/root)
an_attribute = root_node.attributes[1]    # /root/@*[1]

attrs = A('abc') == 'def'                 # @abc='def'
filtered = root_node.children[attrs]      # /root/*[@abc='def']

# The library handles quoting for you:
attr2 = A('abc') == "def'"
filtered2 = root_node.children[attrs]     # /root/*[@abc="def'"]

# You can also use xpath functions:
from xpath.functions import string_length
length = string_length(    # string-length(name(/root/))
is_length_5 = length == 5                 # string-length(name(/root/)) == 5

There is a lot more you can do, check out the main Expression class.

Not all functions are defined in the xpath.functions` module. To add your own, simply define: ``myfunc = Function('my-func'). Please make a merge request if you want some more added to the library!


Manipulating xpath expressions as strings is error-prone and annoying. This library makes it really simple.

It was split out from XCat into a separate module.

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

xpath-expressions-0.2.1.tar.gz (4.3 kB view hashes)

Uploaded source

Built Distribution

xpath_expressions-0.2.1-py3-none-any.whl (8.5 kB view hashes)

Uploaded 3 6

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page