Manipulate xpath expressions in pure python
Project description
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 = 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(root_node.name) # 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!
Why
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
Built Distribution
File details
Details for the file xpath-expressions-0.2.1.tar.gz
.
File metadata
- Download URL: xpath-expressions-0.2.1.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea3c093f876ee681f91931287e7330231cfc7e922dcc59d3c752daafdc855ae0 |
|
MD5 | 0cde85ab9029fb5c6843b3256a805a80 |
|
BLAKE2b-256 | bab3682dd214ffbe5b753aa7466dfca2afbd0895d07957b6a390d694911f4d82 |
File details
Details for the file xpath_expressions-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: xpath_expressions-0.2.1-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46a4fc8235f9178803b9261664cc17944b495c46b1dcd7d986c06f51611be517 |
|
MD5 | ec968c1ebd750bd0fcdf1e03988f1377 |
|
BLAKE2b-256 | d88088e1078092798442e0e5162300fbbd4f2e178c6784f9cb3f36a4507e12e1 |