eulxml.xpath – Parse and Serialize XPath

Functions and classes for parsing XPath expressions into abstract syntax trees and serializing them back to strings.

This module exports two key functions, parse() and serialize().


Parse a string XPath expression into an abstract syntax tree. The AST will be built from the classes defined in eulxml.xpath.ast.


Serialize an XPath AST expressed in terms of eulxml.xpath.ast objects into a valid XPath string.

This module does not support evaluating XPath expressions.

eulxml.xpath.ast – Abstract syntax trees for XPath

Abstract Syntax Tree nodes for parsed XPath.

This module contains basic nodes for representing parsed XPath expressions. The parser provided by this module creates its parsed XPath representation from the classes defined in this module. Library callers will mostly not use this module directly, unless they need to produce XPath ASTs from scratch or perhaps introspect ASTs returned by the parser.


Serialize an XPath AST as a valid XPath expression.

class eulxml.xpath.ast.UnaryExpression(op, right)

A unary XPath expression. Practially, this means -foo.

op = None

the operator used in the expression

right = None

the expression the operator is applied to

class eulxml.xpath.ast.BinaryExpression(left, op, right)

Any binary XPath expression. a/b; a and b; a | b.

left = None

the left side of the binary expression

op = None

the operator of the binary expression

right = None

the right side of the binary expression

class eulxml.xpath.ast.PredicatedExpression(base, predicates=None)

A filtered XPath expression. $var[1]; (a or b)[foo][@bar].

base = None

the base expression to be filtered

predicates = None

a list of filter predicates

class eulxml.xpath.ast.AbsolutePath(op=u'/', relative=None)

An absolute XPath path. /a/b/c; //a/ancestor:b/@c.

op = None

the operator used to root the expression

relative = None

the relative path after the absolute root operator

class eulxml.xpath.ast.Step(axis, node_test, predicates)

A single step in a relative path. a; @b; text(); parent::foo:bar[5].

axis = None

the step’s axis, or @ or None if abbreviated or undefined

node_test = None

a NameTest or NodeType object describing the test represented

predicates = None

a list of predicates filtering the step

class eulxml.xpath.ast.NameTest(prefix, name)

An element name node test for a Step.

name = None

the node name used for the test, or *

prefix = None

the namespace prefix used for the test, or None if unset

class eulxml.xpath.ast.NodeType(name, literal=None)

A node type node test for a Step.

literal = None

the argument to the node specifier. XPath allows these only for processing-instruction() node tests.

name = None

the node type name, such as node or text

class eulxml.xpath.ast.AbbreviatedStep(abbr)

An abbreviated XPath step. . or ..

abbr = None

the abbreviated step

class eulxml.xpath.ast.VariableReference(name)

An XPath variable reference. $foo; $myns:foo.

name = None

a tuple (prefix, localname) containing the variable name

class eulxml.xpath.ast.FunctionCall(prefix, name, args)

An XPath function call. foo(); my:foo(1); foo(1, ‘a’, $var).

args = None

a list of argument expressions

name = None

the local function name

prefix = None

the namespace prefix, or None if unspecified


  • The re standard library module in Python had a bug prior to 2.6.4 that made it reject patterns with Unicode characters above U+FFFF. As a result, XPath expressions including these characters in node names, namespace abbreviations, or function names will not work correctly in those versions of Python.

    If you don’t know what this means, you almost certainly don’t need to worry about it.