Next / Previous / Contents / TCC Help System / NM Tech homepage

5.8. Node.xpath()

It is often convenient to use XPath expressions to quickly locate nodes within a tree. For a description of the XPath language, see XSLT Reference or the standard, XML Path Language (XPath).

This method allows you to evaluate an XPath expression relative to a context node N. The expr argument is an XPath expression string.

N.xpath(expr, nsMap )

The .xpath method returns the result of the evaluation of the XPath expression. For example, if the XPath expression produces a node list, the result will be a Python list containing Node objects. XPath expressions that produce string, numeric, or Boolean results will return Python unicode, float, or boolean values respectively.

The nsMap argument is optional. If provided, it contains a Python dictionary whose keys are namespace prefixes, and each corresponding value is the namespace URI for that prefix. Namespace prefixes in the XPath expression will be translated to namespace URIs using this dictionary.

Here's an example. Suppose roster is an Element node. This expression:

dl = roster.xpath("player[@status='injured']")

would set dl to a list containing all of roster's player child elements that have a status attribute with the value 'injured'.