Unnamed Templates
Unnamed templates include a
matchattribute that contains an XPath expression to specify the criteria for nodes upon which the template should be invoked. In the following example, the template applies to the element namedroutethat is a child of the current context and that has a child element namednext-hopwhose value starts with the string10.10..<xsl:template match="route[starts-with(next-hop, '10.10.')]"><!--... body of the template goes here ...--></xsl:template>By default, when XSLT processes a document, it recursively traverses the entire document hierarchy, inspecting each node, looking for a template that matches the current node. When a matching template is found, the contents of that template are evaluated.
The
<xsl:apply-templates>element can be used inside an unnamed template to limit and control XSLT's default, hierarchical traversal of nodes. Theselectattribute can contain any XPath expression. If the<xsl:apply-templates>element has aselectattribute, only nodes matching the XPath expression defined by the attribute are traversed. If theselectattribute matches no nodes, nothing is traversed and nothing happens. Without aselectattribute, all children of the context node are traversed.In the following example, the template rule matches the
<route>element in the XML hierarchy. All the nodes containing achangedattribute are processed. All<route>elements containing achangedattribute are replaced with a<new>element.<xsl:template match="route"><new><xsl:apply-templates select="*[@changed]"/></new></xsl:template>Using unnamed templates allows the script to ignore where in the XML hierarchy a tag appears. For example, if you want to convert all
<author>tags into<div class="author">tags, using templates enables you to write a single rule that converts all<author>tags, regardless of their location in the input XML document.For more information about how unnamed templates are used in commit scripts, see Importing the junos.xsl File.