Templates
xsl:template
<xsl:template [match="expr" [mode="mode"]] [name="name"]> ... </xsl:template>
- A template is a rule that can be applied to a set of nodes
- Named template: The name attribute allows the template to be
called using call-template
- The use of named templates is comparable to functions (including
support for parameters)
- Can save the return value (the output of the template) by
wrapping the
<xsl:call-template> in a <xsl:variable>
tag
- Either the match attribute or the name attribute must be specified
- The value of the match attribute is an XPath expression
- The mode attribute must be the same as in the corresponding
<xsl:apply-templates>
tag, or it must be absent in both
- The mode can be any arbitrary value
- To apply a template before or after the root element has been
processed, create a template that matches "/"
<xsl:template match="/my_doc_root">
...
</xsl:template>
<xsl:template match="/">
This text will appear before the document root has been processed.
... call the appropriate template here ...
This text will appear after the document root has been processed.
</xsl:template>
<xsl:call-template name="name"/>
<xsl:template name="name">
...
</xsl:template>
xsl:apply-templates
<xsl:apply-templates [select="expr"] [mode="mode"]/>
...
<xsl:template match="element_name">
...
</xsl:template>
- Applies a template rule to the specified nodes or, by default, to
the current node
- The
select attribute can be
used to select specific nodes
- If no matching template is found, the built-in rule is applied to
the nodes
- The mode attribute must be the same as in the corresponding
<xsl:template>
tag, or it must be absent in both
- The mode can be any arbitrary value
<xsl:apply-templates/>
...
<xsl:template match="cd">
Title: <span style="color:#ff0000">
<xsl:value-of select="title"/></span><br />
Artist: <span style="color:#ff0000">
<xsl:value-of select="artist"/></span><br />
</xsl:template>
<xsl:apply-templates select="title"/>
...
<xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
xsl:call-template
<xsl:call-template name="name"/>
- Call a named template
- The name attribute must match a name in an
<xsl:template>
element