cpython: e38f4cf482c7 (original) (raw)

--- a/Doc/library/xml.etree.elementtree.rst +++ b/Doc/library/xml.etree.elementtree.rst @@ -45,10 +45,119 @@ docs. The :mod:xml.etree.cElementTree module is deprecated. +.. elementtree-xpath: + +XPath support +------------- + +This module provides limited support for +XPath expressions <http://www.w3.org/TR/xpath> for locating elements in a +tree. The goal is to support a small subset of the abbreviated syntax; a full +XPath engine is outside the scope of the module. + +Example +^^^^^^^ + +Here's an example that demonstrates some of the XPath capabilities of the +module:: +

+Supported XPath syntax +^^^^^^^^^^^^^^^^^^^^^^ + ++-----------------------+------------------------------------------------------+ +| Syntax | Meaning | ++=======================+======================================================+ +| tag | Selects all child elements with the given tag. | +| | For example, spam selects all child elements | +| | named spam, spam/egg selects all | +| | grandchildren named egg in all children named | +| | spam. | ++-----------------------+------------------------------------------------------+ +| * | Selects all child elements. For example, */egg | +| | selects all grandchildren named egg. | ++-----------------------+------------------------------------------------------+ +| . | Selects the current node. This is mostly useful | +| | at the beginning of the path, to indicate that it's | +| | a relative path. | ++-----------------------+------------------------------------------------------+ +| // | Selects all subelements, on all levels beneath the | +| | current element. For example, ./egg selects | +| | all egg elements in the entire tree. | ++-----------------------+------------------------------------------------------+ +| .. | Selects the parent element. | ++-----------------------+------------------------------------------------------+ +| [@attrib] | Selects all elements that have the given attribute. | ++-----------------------+------------------------------------------------------+ +| [@attrib='value'] | Selects all elements for which the given attribute | +| | has the given value. The value cannot contain | +| | quotes. | ++-----------------------+------------------------------------------------------+ +| [tag] | Selects all elements that have a child named | +| | tag. Only immediate children are supported. | ++-----------------------+------------------------------------------------------+ +| [position] | Selects all elements that are located at the given | +| | position. The position can be either an integer | +| | (1 is the first position), the expression last() | +| | (for the last position), or a position relative to | +| | the last position (e.g. last()-1). | ++-----------------------+------------------------------------------------------+ + +Predicates (expressions within square brackets) must be preceded by a tag +name, an asterisk, or another predicate. position predicates must be +preceded by a tag name. + +Reference +--------- + .. _elementtree-functions: Functions ---------- +^^^^^^^^^ .. function:: Comment(text=None) @@ -199,7 +308,7 @@ Functions .. _elementtree-element-objects: Element Objects ---------------- +^^^^^^^^^^^^^^^ .. class:: Element(tag, attrib={}, **extra) @@ -297,21 +406,24 @@ Element Objects .. method:: find(match) Finds the first subelement matching match. match may be a tag name

.. method:: findall(match)

.. method:: findtext(match, default=None) Finds text for the first subelement matching match. match may be

.. method:: getchildren() @@ -345,8 +457,9 @@ Element Objects .. method:: iterfind(match)

.. versionadded:: 3.2 @@ -391,7 +504,7 @@ Element Objects .. _elementtree-elementtree-objects: ElementTree Objects -------------------- +^^^^^^^^^^^^^^^^^^^ .. class:: ElementTree(element=None, file=None) @@ -413,26 +526,17 @@ ElementTree Objects .. method:: find(match)

.. method:: findall(match)

.. method:: findtext(match, default=None)

.. method:: getiterator(tag=None) @@ -455,9 +559,7 @@ ElementTree Objects .. method:: iterfind(match)

.. versionadded:: 3.2 @@ -512,7 +614,7 @@ Example of changing the attribute "targe .. _elementtree-qname-objects: QName Objects -------------- +^^^^^^^^^^^^^ .. class:: QName(text_or_uri, tag=None) @@ -528,7 +630,7 @@ QName Objects .. _elementtree-treebuilder-objects: TreeBuilder Objects -------------------- +^^^^^^^^^^^^^^^^^^^ .. class:: TreeBuilder(element_factory=None) @@ -579,7 +681,7 @@ TreeBuilder Objects .. _elementtree-xmlparser-objects: XMLParser Objects ------------------ +^^^^^^^^^^^^^^^^^ .. class:: XMLParser(html=0, target=None, encoding=None) @@ -648,7 +750,7 @@ This is an example of counting the maxim 4 Exceptions ----------- +^^^^^^^^^^ .. class:: ParseError