xsl:template
xsl:call-template
xsl:sort
xsl:with-param
ELEMENT: xsl:apply-templates
<xsl:apply-templates
select="expression"
mode="qname"
>
</xsl:apply-templates>
The
xsl:apply-templates
element defines a set of nodes to be processed, or by default selects all child nodes of the current node being processed, and finds a matching template rule to apply to each node in the set. Since each node in the node set is treated individually, it is possible for each node to have a different template applied to it. Note that a template rule is not actually returned, but rather, it manifests itself by how the node is displayed in the output.
There are only two possible procedures by which a template rule can be chosen for a node. If the node matches the pattern defined by the
match
attribute of an
xsl:template
element, then that template will be applied. If more that one such match occurs, then the template with the highest priority will be applied. Or if the priorities are the same, the last template encountered with that priority will be applied. If there are no templates, or a match cannot be found, then the XSLT processor will apply a built-in template rule.
The
xsl:apply-templates
element can only contain the
xsl:sort
or
xsl:with-param
elements. By default the nodes will be assigned templates in the order that they occur. However, if there are one or more
xsl:sort
instructions, then the nodes will be sorted before the templates are assigned. The actual assignment of a template to a specific individual node is not dependent on the sorting order. The
xsl:with-param
element defines parameters that will be applied to the template rules.
This is not a self-closing element. The separate closing element is mandatory.
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:apply-templates
element has a
mode
attribute, then it can only apply template rules from
xsl:template
elements that also have a
mode
attribute.
select="expression"
The optional
select
attribute is set to an expression that returns a node set. This can simply be a string that is the name of a node set. The nodes in the node set are processed in the order that they occur (which is called document order). The default for omitting this attribute is to select all of the child nodes of the current node being processed.
We use the
DevGuru Staff List XML file
for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_applytemplates.xsl"?>
and we name it: xslt_example_applytemplates.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