xsl:apply-templates
xsl:call-template
xsl:stylesheet
xsl:transform
xsl:with-param
ELEMENT: xsl:template
<xsl:template
match="pattern"
mode="qname"
name="qname"
priority="number"
>
</xsl:template>
The
xsl:template
element is used to define a template that can be applied to a node to produce a desired output display.
There must be either a
match
or
name
attribute, or both, and this determines how the template rule can be invoked. If there is only a
match
attribute, then you can use the
xsl:apply-template
element to invoke the template rule defined by the
xsl:template
element. If there is only a
name
attribute, then you can use the
xsl:call-template
element to invoke the named template defined by the
xsl:template
element. If both attributes are present, then you may invoke the template by either procedure.
It is quite possible that more than one template can be applied to a node. The highest
priority
value template is always chosen. If more than one suitable template has the same highest
priority
value, then the XSLT processor usually chooses the one that appears last. Different templates can not have both the same
name
and
priority
values. This is an error.
The
xsl:template
element is always a child of either the
xsl:stylesheet
or
xsl:transform
elements.
This is not a self-closing element. The separate closing element is mandatory.
match="pattern"
The
match
attribute is mandatory unless the element has a
name
attribute, then this attribute is optional. In other words, there must be either a
match
or
name
attribute, or both. This attribute is a pattern that is used to define which nodes will have which template rules applied to them.
mode="qname"
The optional
mode
attribute allows the same nodes to be processed more than once. Each time the nodes are processed, they can be displayed in a different manner. A qname is a qualified name that is composed of an optional namespace prefix, a colon, which is only present if there is a prefix, and a mandatory XML name (for example, xsl:zipcode or zipcode). If an
xsl:template
element has a
mode
attribute, then it must also have a
match
attribute. If the
xsl:apply-templates
element has a
mode
attribute, then it can only apply templates from
xsl:templates
elements that also have a
mode
attribute. Likewise, if the
xsl:apply-templates
element does not have a
mode
attribute, then it can only apply templates from
xsl:templates
elements that also do not have a
mode
attribute.
name="qname"
The
name
attribute is mandatory unless the element has a
match
attribute, then this attribute is optional. In other words, there must be either a
match
or
name
attribute, or both. A qname is a qualified name that is composed of an optional namespace prefix, a colon which is only present if there is a prefix, and a mandatory XML name (for example, xsl:zipcode or zipcode). If the
name
attribute is present, you can use the
xsl:call-template
element to invoke the template. To do this, the
name
attribute must be the same for both the
xsl:template
and the
xsl:call-template
elements.
priority="number"
The optional
priority
attribute is a real number that ranges from -9.0 to 0.0 to 9.0 that sets the priority of importance for a template. The higher the number, the higher the priority. If more than one template is suitable for a node, then the highest priority template is always chosen. The most common default value is 0 (zero), however the exact default value that will be assigned by the XSLT processor is dependent on the pattern information contained in the
match
attribute.
We use the
DevGuru Staff List XML file
for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_template.xsl"?>
and we name it: xslt_example_template.xml
In this example we create three different templates and apply them in the desired order in a fourth template using the
xsl:apply-templates
element.
Code for xslt_example_template.xsl:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<
xsl:template match="devguru_staff/programmer
>
<html>
<body>
<xsl:apply-templates select="name" />
<xsl:apply-templates select="dob" />
<xsl:apply-templates select="age" />
<br />
</body>
</html>
<
/xsl:template
>
<
xsl:template match="name"
>
<span style="font-size=22px;">
<xsl:value-of select="." />
</span>
<br />
<
/xsl:template
>
<
xsl:template match="dob"
>
DOB: <span style="color:blue;">
<xsl:value-of select="." />
</span>
<br />
<
/xsl:template
>
<
xsl:template match="age"
>
AGE: <span style="color:green;">
<xsl:value-of select="." />
</span>
<br />
<
/xsl:template
>
</xsl:stylesheet>
Output:
Click to view output in separate window
- requires Internet Explorer
Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information