Skip to main content

Treat XPath expressions as Python objects

Project description

XPath-expressions

https://travis-ci.org/orf/xpath-expressions https://pypi.org/project/xpath-expressions/

This is a small, lightweight Python 3.5+ library to aide in the manipulations of xpath expressions. It allows you to manipulate them as Python objects with Python expressions and operators.

Install

pip install xpath-expressions

Quickstart

from xpath import Expression, Attribute

root_node = Expression('/root')
print(root_node.children)                 # /root/*
print(root_node.name)                     # name(/root)
print(root_node.attributes[1])            # /root/@*[1]
print(root_node / 'abc' / 'def')          # /root/abc/def

# Filtering expressions:
print(root_node.text == 'abc')            # /root/text()='abc'

expr = Attribute('abc') == 'def'
print(expr)                               # @abc='def'
print(root_node.children[expr])           # /root/*[@abc='def']

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

# You can use xpath functions:
from xpath import func
# Pass arguments like usual
expr = func.string_length(root_node.name)
print(expr)                               # string-length(name(/root/))

# And treat those as normal expressions
print(expr == 5)                          # string-length(name(/root/)) == 5

# The library normalizes python reserved names:
print(func.or_())                         # or()

# Use custom namespaces
from xpath import Functions
ns_functions = Functions('my-ns:')
print(ns_functions.something())           # my-ns:something()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for xpath-expressions, version 1.0.2
Filename, size File type Python version Upload date Hashes
Filename, size xpath_expressions-1.0.2-py3-none-any.whl (5.3 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size xpath-expressions-1.0.2.tar.gz (3.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page