RexE
RexE

Reputation: 17723

ElementTree: What is the syntax for the 'match' argument to the 'find' method?

The official documentation here just says, "match may be a tag name or path", but I don't see a definition of "path" anywhere. From looking at examples on the web I gather it's some stripped-down XPath-like notation, but it's unclear exactly what is allowed -- for example, should the path start with a /, a //, or no delimiter at all? Can I specify attributes with [@att = "value"]?

Upvotes: 2

Views: 1023

Answers (1)

li.davidm
li.davidm

Reputation: 12116

Well, looking at the source code at http://hg.python.org/cpython/file/2.7/Lib/xml/etree/ElementTree.py we find that Element.find is implemented as

def find(self, path, namespaces=None):
    return ElementPath.find(self, path, namespaces)

ElementPath is implemented as

try:
    from . import ElementPath
except ImportError:
    ElementPath = _SimpleElementPath()

_SimpleElementPath only checks tag name:

# emulate pre-1.2 find/findtext/findall behaviour
def find(self, element, tag, namespaces=None):
    for elem in element:
        if elem.tag == tag:
            return elem
    return None

So let's look at ElementPath.py: http://hg.python.org/cpython/file/f98e2944cb40/Lib/xml/etree/ElementPath.py It states,

# limited xpath support for element trees

So I would assume valid XPath is likely a valid argument for find. I'm not familiar enough with XPath to determine exactly what it supports, but http://effbot.org/zone/element-xpath.htm describes how much it supported five years ago and includes a table of syntax.

ElementTree provides limited support for XPath expressions. The goal is to support a small subset of the abbreviated syntax; a full XPath engine is outside the scope of the core library.

http://docs.python.org/dev/library/xml.etree.elementtree.html#xpath-support has a more updated table. It doesn't look too different.

Upvotes: 3

Related Questions