Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

XPath Overview

XSLT uses the XML Path Language (XPath) standard to specify and locate elements in the input document’s XML hierarchy. XPath’s powerful expression syntax enables you to define complex criteria for selecting portions of the XML input document.

Nodes and Axes

XPath views every piece of the document hierarchy as a node. For commit scripts, op scripts, event scripts, and SNMP scripts, the important types of nodes are element nodes, text nodes, and attribute nodes. Consider the following XML tags:

These XML tag elements show examples of the following types of XPath nodes:

  • <host-name>my-router</host-name>—Element node

  • my-router—Text node

  • inactive="inactive"—Attribute node

XPath views nodes as being arranged in certain axes. The following list includes a few common axes. However, XPath has numerous other axes that are not listed here.

  • ancestor—Points from a node up through its series of parent nodes

  • child—Points through the list of an element node’s direct child nodes

  • attribute—Points through the list of an element node’s set of attributes

  • following-sibling—Points through the nodes that follow a node but are under the same parent

  • descendant—Contains all the descendents of a node

XSLT evaluates each XPath expression from a particular node, which is referred to as the context node (or simply context). The context node is the node at which the XSLT processor is currently looking. XSLT changes the context as it traverses the document’s hierarchy. It evaluates XPath expressions from that particular context node.

Note:

In Junos OS commit scripts, the context node concept corresponds to Junos OS hierarchy levels. For example, the /configuration/system/domain-name XPath expression sets the context node to the [edit system domain-name] hierarchy level.

We recommend including the <xsl:template match="configuration"> template in all commit scripts. This element enables you to exclude the /configuration/ root element from all XPath expressions in programming instructions (such as <xsl:for-each> or <xsl:if>) in the script. Thus, XPath expressions can begin at a Junos hierarchy level (for example, system/domain-name). For more information, see Required Boilerplate for Commit Scripts.

Path and Predicate Syntax

An XPath expression contains two types of syntax: a path syntax and a predicate syntax. Path syntax specifies which nodes to inspect in terms of their path locations on one of the axes in the document’s hierarchy from the current context node. Several examples of path syntax follow:

  • accounting-options—Selects an element node named accounting-options that is a child of the current context.

  • server/name—Selects an element node named name that is a child of an element named server that is a child of the current context.

  • /configuration/system/domain-name—Selects an element node named domain-name that is the child of an element named system that is the child of the root element of the document (configuration).

  • parent::system/host-name—Selects an element node named host-name that is the child of an element named system that is the parent of the current context node. You can abbreviate the parent:: axis as two periods (..).

The predicate syntax allows you to perform tests at each node selected by the path syntax. The result set includes only those nodes that pass the test. A predicate appears inside square brackets ([ ]) after a path node. Following are several examples of predicate syntax:

  • server[name = '10.1.1.1']—Selects an element named server that is a child of the current context and has a child element named name whose value is 10.1.1.1.

  • *[@inactive]—Selects any node (* matches any node) that is a child of the current context and that has an attribute (@ selects nodes from the attribute axis) named inactive.

  • route[starts-with(next-hop, '10.10.')]—Selects an element named route that is a child of the current context and that has a child element named next-hop whose value starts with the string 10.10..

    The starts-with function is one of many functions that are built into XPath. XPath also supports relational tests, equality tests, and many more features not listed here.

XPath Operators

XPath supports many operators, for example:

  • Logical operators, such as AND and | (or)

  • Comparison operators, such as =, !=, <, and >

  • Numerical operators, such as +, -, and *

XSLT scripts are XML documents. In XSLT, you must represent the less-than signs in the same way as you do in XML. Thus, you must represent the less-than (<) operator as &lt; and the less-than-or-equal-to (<=) operator as &lt;=.

For more information about XPath functions and operators, consult a comprehensive XPath reference guide. XPath is fully described in the W3C specification at http://w3c.org/TR/xpath.