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

Nodes are viewed as being arranged in certain axes. The ancestor axis points from a node up through its series of parent nodes. The child axis points through the list of an element node’s direct child nodes. The attribute axis points through the list of an element node’s set of attributes. The following-sibling axis points through the nodes that follow a node but are under the same parent. The descendant axis contains all the descendents of a node. There are numerous other axes that are not listed here.

Each XPath expression is evaluated 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 the document’s hierarchy is traversed, and XPath expressions are evaluated from that particular context node.


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 allows 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 allowing you to begin XPath expressions 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. The parent:: axis can be abbreviated as two periods (..).

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

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

  • *[@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 standard logical operators, such as AND and | (or); comparison operators, such as =, !=, <, and >; and numerical operators, such as +, -, and *.

In XSLT, you always have to represent the less-than (<) operator as &lt; and the less-than-or-equal-to (<=) operator as &lt;= because XSLT scripts are XML documents, and less-than signs are represented this way in XML.

For more information about XPath functions and operators, consult a comprehensive XPath reference guide. XPath is fully described in the W3C specification at