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:
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
/configuration/ root element from all
XPath expressions in programming instructions (such as
<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-optionsthat is a child of the current context.
server/name—Selects an element node named
namethat is a child of an element named
serverthat is a child of the current context.
/configuration/system/domain-name—Selects an element node named
domain-namethat is the child of an element named
systemthat is the child of the root element of the document (
parent::system/host-name—Selects an element node named
host-namethat is the child of an element named
systemthat 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 = '10.1.1.1']—Selects an element named
serverthat is a child of the current context and has a child element named
namewhose 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
route[starts-with(next-hop, '10.10.')]—Selects an element named
routethat is a child of the current context and that has a child element named
next-hopwhose value starts with the string 10.10..
starts-withfunction 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 supports standard logical operators, such as
| (or); comparison
operators, such as
>; and numerical operators, such as
In XSLT, you always have to represent the less-than (
<) operator as
< and the less-than-or-equal-to (
<= because XSLT scripts
are XML documents, and less-than signs are represented this way in
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.