Python Xpath Syntax
xml.etree.ElementTree — The ElementTree XML API …
Source code: Lib/xml/etree/
The module implements a simple and efficient API
for parsing and creating XML data.
Changed in version 3. 3: This module will use a fast implementation whenever available.
Deprecated since version 3. 3: The module is deprecated.
Warning
The module is not secure against
maliciously constructed data. If you need to parse untrusted or
unauthenticated data see XML vulnerabilities.
Tutorial¶
This is a short tutorial for using (ET in
short). The goal is to demonstrate some of the building blocks and basic
concepts of the module.
XML tree and elements¶
XML is an inherently hierarchical data format, and the most natural way to
represent it is with a tree. ET has two classes for this purpose –
ElementTree represents the whole XML document as a tree, and
Element represents a single node in this tree. Interactions with
the whole document (reading and writing to/from files) are usually done
on the ElementTree level. Interactions with a single XML element
and its sub-elements are done on the Element level.
Parsing XML¶
We’ll be using the following XML document as the sample data for this section:
xml version="1. 0"? >
We can import this data by reading from a file:
import as ET
tree = (”)
root = troot()
Or directly from a string:
root = omstring(country_data_as_string)
fromstring() parses XML from a string directly into an Element,
which is the root element of the parsed tree. Other parsing functions may
create an ElementTree. Check the documentation to be sure.
As an Element, root has a tag and a dictionary of attributes:
>>>
‘data’
{}
It also has children nodes over which we can iterate:
>>> for child in root:… print(, )…
country {‘name’: ‘Liechtenstein’}
country {‘name’: ‘Singapore’}
country {‘name’: ‘Panama’}
Children are nested, and we can access specific child nodes by index:
>>> root[0][1]
‘2008’
Note
Not all elements of the XML input will end up as elements of the
parsed tree. Currently, this module skips over any XML comments,
processing instructions, and document type declarations in the
input. Nevertheless, trees built using this module’s API rather
than parsing from XML text can have comments and processing
instructions in them; they will be included when generating XML
output. A document type declaration may be accessed by passing a
custom TreeBuilder instance to the XMLParser
constructor.
Pull API for non-blocking parsing¶
Most parsing functions provided by this module require the whole document
to be read at once before returning any result. It is possible to use an
XMLParser and feed data into it incrementally, but it is a push API that
calls methods on a callback target, which is too low-level and inconvenient for
most needs. Sometimes what the user really wants is to be able to parse XML
incrementally, without blocking operations, while enjoying the convenience of
fully constructed Element objects.
The most powerful tool for doing this is XMLPullParser. It does not
require a blocking read to obtain the XML data, and is instead fed with data
incrementally with () calls. To get the parsed XML
elements, call ad_events(). Here is an example:
>>> parser = ET. XMLPullParser([‘start’, ‘end’])
>>> (‘
>>> list(ad_events())
[(‘start’,
>>> (‘ more text
>>> for event, elem in ad_events():… print(event)… print(, ‘text=’, )…
end
The obvious use case is applications that operate in a non-blocking fashion
where the XML data is being received from a socket or read incrementally from
some storage device. In such cases, blocking reads are unacceptable.
Because it’s so flexible, XMLPullParser can be inconvenient to use for
simpler use-cases. If you don’t mind your application blocking on reading XML
data but would still like to have incremental parsing capabilities, take a look
at iterparse(). It can be useful when you’re reading a large XML document
and don’t want to hold it wholly in memory.
Finding interesting elements¶
Element has some useful methods that help iterate recursively over all
the sub-tree below it (its children, their children, and so on). For example,
():
>>> for neighbor in (‘neighbor’):… print()…
{‘name’: ‘Austria’, ‘direction’: ‘E’}
{‘name’: ‘Switzerland’, ‘direction’: ‘W’}
{‘name’: ‘Malaysia’, ‘direction’: ‘N’}
{‘name’: ‘Costa Rica’, ‘direction’: ‘W’}
{‘name’: ‘Colombia’, ‘direction’: ‘E’}
ndall() finds only elements with a tag which are direct
children of the current element. () finds the first child
with a particular tag, and accesses the element’s text
content. () accesses the element’s attributes:
>>> for country in ndall(‘country’):… rank = (‘rank’)… name = (‘name’)… print(name, rank)…
Liechtenstein 1
Singapore 4
Panama 68
More sophisticated specification of which elements to look for is possible by
using XPath.
Modifying an XML File¶
ElementTree provides a simple way to build XML documents and write them to files.
The () method serves this purpose.
Once created, an Element object may be manipulated by directly changing
its fields (such as), adding and modifying attributes
(() method), as well as adding new children (for example
with ()).
Let’s say we want to add one to each country’s rank, and add an updated
attribute to the rank element:
>>> for rank in (‘rank’):… new_rank = int() + 1… = str(new_rank)… (‘updated’, ‘yes’)…
>>> (”)
Our XML now looks like this:
We can remove elements using (). Let’s say we want to
remove all countries with a rank higher than 50:
>>> for country in ndall(‘country’):… # using ndall() to avoid removal during traversal… rank = int((‘rank’))… if rank > 50:… (country)…
Note that concurrent modification while iterating can lead to problems,
just like when iterating and modifying Python lists or dicts.
Therefore, the example first collects all matching elements with
ndall(), and only then iterates over the list of matches.
Building XML documents¶
The SubElement() function also provides a convenient way to create new
sub-elements for a given element:
>>> a = ET. Element(‘a’)
>>> b = bElement(a, ‘b’)
>>> c = bElement(a, ‘c’)
>>> d = bElement(c, ‘d’)
>>> (a)
Parsing XML with Namespaces¶
If the XML input has namespaces, tags and attributes
with prefixes in the form prefix:sometag get expanded to
{uri}sometag where the prefix is replaced by the full URI.
Also, if there is a default namespace,
that full URI gets prepended to all of the non-prefixed tags.
Here is an XML example that incorporates two namespaces, one with the
prefix “fictional” and the other serving as the default namespace:
By default, the href attribute is treated as a file name. You can use custom loaders to override this behaviour. Also note that the standard helper does not support XPointer syntax.
To process this file, load it as usual, and pass the root element to the module:
from import ElementTree, ElementInclude
tree = (“”)
clude(root)
The ElementInclude module replaces the {include element with the root element from the document. The result might look something like this:
To include a text document, use the {include element, and set the parse attribute to “text”:
Copyright (c)
The result might look something like:
Copyright (c) 2003.
(href, parse, encoding=None)¶
Default loader. This default loader reads an included resource from disk. href is a URL.
parse is for parse mode either “xml” or “text”. encoding
is an optional text encoding. If not given, encoding is utf-8. Returns the
expanded resource. If the parse mode is “xml”, this is an ElementTree
instance. If the parse mode is “text”, this is a Unicode string. If the
loader fails, it can return None or raise an exception.
(elem, loader=None, base_url=None, max_depth=6)¶
This function expands XInclude directives. elem is the root element. loader is
an optional resource loader. If omitted, it defaults to default_loader().
If given, it should be a callable that implements the same interface as
default_loader(). base_url is base URL of the original file, to resolve
relative include file references. max_depth is the maximum number of recursive
inclusions. Limited to reduce the risk of malicious content explosion. Pass a
negative value to disable the limitation.
Returns the expanded resource. If the parse mode is
“xml”, this is an ElementTree instance. If the parse mode is “text”,
this is a Unicode string. If the loader fails, it can return None or
raise an exception.
New in version 3. 9: The base_url and max_depth parameters.
Element Objects¶
class (tag, attrib={}, **extra)¶
Element class. This class defines the Element interface, and provides a
reference implementation of this interface.
bytestrings or Unicode strings. tag is the element name. attrib is
an optional dictionary, containing element attributes. extra contains
additional attributes, given as keyword arguments.
tag¶
A string identifying what kind of data this element represents (the
element type, in other words).
text¶
tail¶
These attributes can be used to hold additional data associated with
the element. Their values are usually strings but may be any
application-specific object. If the element is created from
an XML file, the text attribute holds either the text between
the element’s start tag and its first child or end tag, or None, and
the tail attribute holds either the text between the element’s
end tag and the next tag, or None. For the XML data
1
the a element has None for both text and tail attributes,
the b element has text “1” and tail “4”,
the c element has text “2” and tail None,
and the d element has text None and tail “3”.
To collect the inner text of an element, see itertext(), for
example “”(ertext()).
Applications may store arbitrary objects in these attributes.
attrib¶
A dictionary containing the element’s attributes. Note that while the
attrib value is always a real mutable Python dictionary, an ElementTree
implementation may choose to use another internal representation, and
create the dictionary only if someone asks for it. To take advantage of
such implementations, use the dictionary methods below whenever possible.
The following dictionary-like methods work on the element attributes.
clear()¶
Resets an element. This function removes all subelements, clears all
attributes, and sets the text and tail attributes to None.
get(key, default=None)¶
Gets the element attribute named key.
Returns the attribute value, or default if the attribute was not found.
items()¶
Returns the element attributes as a sequence of (name, value) pairs. The
attributes are returned in an arbitrary order.
keys()¶
Returns the elements attribute names as a list. The names are returned
in an arbitrary order.
set(key, value)¶
Set the attribute key on the element to value.
The following methods work on the element’s children (subelements).
append(subelement)¶
Adds the element subelement to the end of this element’s internal list
of subelements. Raises TypeError if subelement is not an
Element.
extend(subelements)¶
Appends subelements from a sequence object with zero or more elements.
Raises TypeError if a subelement is not an Element.
find(match, namespaces=None)¶
Finds the first subelement matching match. match may be a tag name
or a path. Returns an element instance
or None. namespaces is an optional mapping from namespace prefix
to full name. Pass ” as prefix to move all unprefixed tag names
in the expression into the given namespace.
findall(match, namespaces=None)¶
Finds all matching subelements, by tag name or
path. Returns a list containing all matching
elements in document order. namespaces is an optional mapping from
namespace prefix to full name. Pass ” as prefix to move all
unprefixed tag names in the expression into the given namespace.
findtext(match, default=None, namespaces=None)¶
Finds text for the first subelement matching match. match may be
a tag name or a path. Returns the text content
of the first matching element, or default if no element was found.
Note that if the matching element has no text content an empty string
is returned. namespaces is an optional mapping from namespace prefix
insert(index, subelement)¶
Inserts subelement at the given position in this element. Raises
TypeError if subelement is not an Element.
iter(tag=None)¶
Creates a tree iterator with the current element as the root.
The iterator iterates over this element and all elements below it, in
document (depth first) order. If tag is not None or ‘*’, only
elements whose tag equals tag are returned from the iterator. If the
tree structure is modified during iteration, the result is undefined.
iterfind(match, namespaces=None)¶
path. Returns an iterable yielding all
matching elements in document order. namespaces is an optional mapping
from namespace prefix to full name.
itertext()¶
Creates a text iterator. The iterator loops over this element and all
subelements, in document order, and returns all inner text.
makeelement(tag, attrib)¶
Creates a new element object of the same type as this element. Do not
call this method, use the SubElement() factory function instead.
remove(subelement)¶
Removes subelement from the element. Unlike the find* methods this
method compares elements based on the instance identity, not on tag value
or contents.
Element objects also support the following sequence type methods
for working with subelements: __delitem__(),
__getitem__(), __setitem__(),
__len__().
Caution: Elements with no subelements will test as False. This behavior
will change in future versions. Use specific len(elem) or elem is
None test instead.
element = (‘foo’)
if not element: # careful!
print(“element not found, or element has no subelements”)
if element is None:
print(“element not found”)
Prior to Python 3. 8, the serialisation order of the XML attributes of
elements was artificially made predictable by sorting the attributes by
their name. Based on the now guaranteed ordering of dicts, this arbitrary
reordering was removed in Python 3. 8 to preserve the order in which
attributes were originally parsed or created by user code.
In general, user code should try not to depend on a specific ordering of
attributes, given that the XML Information Set explicitly excludes the attribute
order from conveying information. Code should be prepared to deal with
any ordering on input. In cases where deterministic XML output is required,
e. for cryptographic signing or test data sets, canonical serialisation
is available with the canonicalize() function.
In cases where canonical output is not applicable but a specific attribute
order is still desirable on output, code should aim for creating the
attributes directly in the desired order, to avoid perceptual mismatches
for readers of the code. In cases where this is difficult to achieve, a
recipe like the following can be applied prior to serialisation to enforce
an order independently from the Element creation:
def reorder_attributes(root):
for el in ():
attrib =
if len(attrib) > 1:
# adjust attribute order, e. by sorting
attribs = sorted(())
()
(attribs)
ElementTree Objects¶
class (element=None, file=None)¶
ElementTree wrapper class. This class represents an entire element
hierarchy, and adds some extra support for serialization to and from
standard XML.
element is the root element. The tree is initialized with the contents
of the XML file if given.
_setroot(element)¶
Replaces the root element for this tree. This discards the current
contents of the tree, and replaces it with the given element. Use with
care. element is an element instance.
Same as (), starting at the root of the tree.
Same as ndall(), starting at the root of the tree.
Same as ndtext(), starting at the root of the tree.
getroot()¶
Returns the root element for this tree.
Creates and returns a tree iterator for the root element. The iterator
loops over all elements in this tree, in section order. tag is the tag
to look for (default is to return all elements).
Same as erfind(), starting at the root of the tree.
parse(source, parser=None)¶
Loads an external XML section into this element tree. source is a file
name or file object. parser is an optional parser instance.
If not given, the standard XMLParser parser is used. Returns the
section root element.
write(file, encoding=”us-ascii”, xml_declaration=None, default_namespace=None, method=”xml”, *, short_empty_elements=True)¶
Writes the element tree to a file, as XML. file is a file name, or a
file object opened for writing. encoding 1 is the output
encoding (default is US-ASCII).
xml_declaration controls if an XML declaration should be added to the
file. Use False for never, True for always, None
for only if not US-ASCII or UTF-8 or Unicode (default is None).
default_namespace sets the default XML namespace (for “xmlns”).
method is either “xml”, “html” or “text” (default is
“xml”).
The keyword-only short_empty_elements parameter controls the formatting
of elements that contain no content. If True (the default), they are
emitted as a single self-closed tag, otherwise they are emitted as a pair
of start/end tags.
The output is either a string (str) or binary (bytes).
This is controlled by the encoding argument. If encoding is
“unicode”, the output is a string; otherwise, it’s binary. Note that
this may conflict with the type of file if it’s an open
file object; make sure you do not try to write a string to a
binary stream and vice versa.
Changed in version 3. 8: The write() method now preserves the attribute order specified
This is the XML file that is going to be manipulated:
Example of changing the attribute “target” of every link in first paragraph:
>>> from import ElementTree
>>> tree = ElementTree()
>>> (“”)
>>> p = (“body/p”) # Finds first occurrence of tag p in body
>>> p
>>> links = list((“a”)) # Returns list of all links
>>> links
[
>>> for i in links: # Iterates through all found links… [“target”] = “blank”
QName Objects¶
class (text_or_uri, tag=None)¶
QName wrapper. This can be used to wrap a QName attribute value, in order
to get proper namespace handling on output. text_or_uri is a string
containing the QName value, in the form {uri}local, or, if the tag argument
is given, the URI part of a QName. If tag is given, the first argument is
interpreted as a URI, and this argument is interpreted as a local name.
QN
How to use XPath in Python? – Stack Overflow
What are the libraries that support XPath? Is there a full implementation? How is the library used? Where is its website?
funnydman3, 4873 gold badges17 silver badges31 bronze badges
asked Aug 12 ’08 at 11:28
4
libxml2 has a number of advantages:
Compliance to the spec
Active development and a community participation
Speed. This is really a python wrapper around a C implementation.
Ubiquity. The libxml2 library is pervasive and thus well tested.
Downsides include:
Compliance to the spec. It’s strict. Things like default namespace handling are easier in other libraries.
Use of native code. This can be a pain depending on your how your application is distributed / deployed. RPMs are available that ease some of this pain.
Manual resource handling. Note in the sample below the calls to freeDoc() and xpathFreeContext(). This is not very Pythonic.
If you are doing simple path selection, stick with ElementTree ( which is included in Python 2. 5). If you need full spec compliance or raw speed and can cope with the distribution of native code, go with libxml2.
Sample of libxml2 XPath Use
import libxml2
doc = rseFile(“”)
ctxt = doc. xpathNewContext()
res = ctxt. xpathEval(“//*”)
if len(res)! = 2:
print “xpath query: wrong node set size”
(1)
if res[0]! = “doc” or res[1]! = “foo”:
print “xpath query: wrong node set value”
eeDoc()
ctxt. xpathFreeContext()
Sample of ElementTree XPath Use
from elementtree. ElementTree import ElementTree
mydoc = ElementTree(file=”)
for e in ndall(‘/foo/bar’):
print (‘title’)
Markus Safar5, 9735 gold badges25 silver badges41 bronze badges
answered Aug 26 ’08 at 13:06
Ryan CoxRyan Cox4, 8772 gold badges23 silver badges18 bronze badges
3
The lxml package supports xpath. It seems to work pretty well, although I’ve had some trouble with the self:: axis. There’s also Amara, but I haven’t used it personally.
MvG52. 8k15 gold badges129 silver badges257 bronze badges
answered Aug 12 ’08 at 11:40
James SulakJames Sulak28. 8k11 gold badges50 silver badges56 bronze badges
Sounds like an lxml advertisement in here. 😉 ElementTree is included in the std library. Under 2. 6 and below its xpath is pretty weak, but in 2. 7+ much improved:
import as ET
root = (filename)
result = ”
for elem in ndall(‘. //child/grandchild’):
# How to make decisions based on attributes even in 2. 6:
if (‘name’) == ‘foo’:
result =
break
answered Nov 22 ’12 at 1:05
Gringo SuaveGringo Suave26. 2k6 gold badges79 silver badges71 bronze badges
Use LXML. LXML uses the full power of libxml2 and libxslt, but wraps them in more “Pythonic” bindings than the Python bindings that are native to those libraries. As such, it gets the full XPath 1. 0 implementation. Native ElemenTree supports a limited subset of XPath, although it may be good enough for your needs.
answered Nov 13 ’09 at 23:11
user210794user2107941, 0118 silver badges3 bronze badges
Another option is py-dom-xpath, it works seamlessly with minidom and is pure Python so works on appengine.
import xpath
(‘//item’, doc)
answered Jan 23 ’10 at 9:30
SamSam6, 1204 gold badges40 silver badges52 bronze badges
You can use:
PyXML:
from import Sax2
from xml import xpath
doc = omXmlFile(”). documentElement
for url in xpath. Evaluate(‘//@Url’, doc):
print
libxml2:
doc = rseFile(”)
for url in doc. xpathEval(‘//@Url’):
print ntent
answered Aug 23 ’10 at 13:00
0xAX0xAX19. 1k24 gold badges108 silver badges195 bronze badges
1
You can use the simple soupparser from lxml
Example:
from import fromstring
tree = fromstring(“Find me! “)
print (“//a/text()”)
answered Nov 15 ’15 at 5:31
Aminah NurainiAminah Nuraini14. 8k7 gold badges76 silver badges97 bronze badges
2
The latest version of elementtree supports XPath pretty well. Not being an XPath expert I can’t say for sure if the implementation is full but it has satisfied most of my needs when working in Python. I’ve also use lxml and PyXML and I find etree nice because it’s a standard module.
NOTE: I’ve since found lxml and for me it’s definitely the best XML lib out there for Python. It does XPath nicely as well (though again perhaps not a full implementation).
answered Aug 14 ’08 at 9:48
jkpjkp72. 2k26 gold badges99 silver badges102 bronze badges
If you want to have the power of XPATH combined with the ability to also use CSS at any point you can use parsel:
>>> from parsel import Selector
>>> sel = Selector(text=u”””
Hello, Parsel!
“””)
>>>
>>> (‘h1::text’). extract_first()
‘Hello, Parsel! ‘
>>> (‘//h1/text()’). extract_first()
answered Dec 16 ’17 at 22:16
eLRuLLeLRuLL17. 4k8 gold badges68 silver badges94 bronze badges
Another library is 4Suite:
I do not know how spec-compliant it is. But it has worked very well for my use. It looks abandoned.
answered Aug 23 ’10 at 12:57
codeapecodeape91. 6k22 gold badges145 silver badges175 bronze badges
PyXML works well.
You didn’t say what platform you’re using, however if you’re on Ubuntu you can get it with sudo apt-get install python-xml. I’m sure other Linux distros have it as well.
If you’re on a Mac, xpath is already installed but not immediately accessible. You can set PY_USE_XMLPLUS in your environment or do it the Python way before you import
if artswith(‘darwin’):
os. environ[‘PY_USE_XMLPLUS’] = ‘1’
In the worst case you may have to build it yourself. This package is no longer maintained but still builds fine and works with modern 2. x Pythons. Basic docs are here.
answered Aug 12 ’08 at 19:34
David JoynerDavid Joyner20. 4k4 gold badges26 silver badges33 bronze badges
If you are going to need it for html:
import as html
root = omstring(string)
(‘//meta’)
answered May 29 ’19 at 13:48
Thomas G. 2, 18417 silver badges19 bronze badges
XPath Tutorial – W3Schools
What is XPath?
XPath is a major element in the XSLT standard.
XPath can be used to navigate through elements and attributes in an XML document.
XPath stands for XML Path Language
XPath uses “path like” syntax to identify and navigate nodes in an XML document
XPath contains over 200 built-in functions
XPath is a major element in the XSLT standard
XPath is a W3C recommendation
XPath Path Expressions
XPath uses path expressions to select nodes or node-sets in an XML document.
These path expressions look very much like the path expressions you use with
traditional computer file systems:
XPath Standard Functions
XPath includes over 200 built-in functions.
There are functions for string values, numeric values, booleans, date and time comparison,
node manipulation, sequence manipulation, and much more.
Today XPath expressions can also be used in JavaScript, Java, XML Schema, PHP, Python,
C and C++, and lots of other languages.
XPath is Used in XSLT
With XPath knowledge you will be able to take great advantage of your XSLT
knowledge.
XPath is a W3C Recommendation
XPath 1. 0 became a W3C Recommendation on November 16, 1999.
XPath 2. 0 became a W3C Recommendation on January 23, 2007.
XPath 3. 0 became a W3C Recommendation on April 8, 2014.
Frequently Asked Questions about python xpath syntax
What is XPath syntax?
What Is XPath? XPath is defined as XML path. It is a syntax or language for finding any element on the web page using the XML path expression. … In Selenium automation, if the elements are not found by the general locators like ID, class, name, etc., then XPath is used to find an element on the web page.May 28, 2020
How do you add XPath in python?
These can be installed in the command line using the pip package installer for Python….To find the XPath for a particular element on a page:Right-click the element in the page and click on Inspect.Right click on the element in the Elements Tab.Click on copy XPath.Oct 11, 2020
What is an XPath in python?
Xpath is one the locators used in Selenium to identify elements uniquely on a web page. It traverses the DOM to reach the desired element having a particular attribute with/without tagname. The xpath can represented by the ways listed below −Jul 29, 2020