[Contents] [Prev] [Next] [Index] [Report an Error]

Unnamed Templates

Unnamed templates include a match attribute 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 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..

<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. The select attribute can contain any XPath expression. If the <xsl:apply-templates> element has a select attribute, only nodes matching the XPath expression defined by the attribute are traversed. If the select attribute matches no nodes, nothing is traversed and nothing happens. Without a select attribute, 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 a changed attribute are processed. All <route> elements containing a changed attribute 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.


[Contents] [Prev] [Next] [Index] [Report an Error]