At run-time, the XSLT processor associates the templates in an XSLT file with specific source nodes, starting with the template that matches the document root node (match="/"
). When the processor begins to process a template, the source node associated with that template becomes the current node. This current node defines a context node for evaluating the remaining XML Path Language (XPath) expressions within the template.
As the XSLT processor evaluates the XPath expressions within a template, the context node often changes based upon the results of the evaluation.
Having defined the current node, the XSLT processor begins evaluating the XPath expressions within the template.
The following XSLT elements accept XPath expressions as attribute values: <xsl:value-of>
, <xsl:for-each>
, <xsl:apply-templates>
, <xsl:if>
, <xsl:when>
, <xsl:copy-of>
, <xsl:param>
, <xsl:sort>
, <xsl:variable>
, <xsl:with-param>
, and <xsl:template>
. The attribute names define how the context for the query is determined and whether it results in a new context.
The <xsl:value-of>
, <xsl:for-each>
, <xsl:copy-of>
, <xsl:param>
, <xsl:sort>
, <xsl:variable>
, <xsl:with-param>
, and <xsl:apply-templates>
elements have a select
attribute. This expression is evaluated relative to the template's current node. In <xsl:for-each>
or <xsl:apply-templates>
, the select returns a node-set, and each node in this set becomes the current node for further queries within it.
The following example shows <xsl:for-each>
and <xsl:value-of>
elements are used in a simplified snippet from invoice.xsl (in Sample XSLT File for DOM Context).
<TABLE BORDER="1"> <xsl:for-each select="invoices/invoice"> <TR> <TD>Invoice #<xsl:value-of select="@id"/></TD> </TR> <xsl:for-each select="items/item"> <TR> <TD><xsl:value-of select="qty"/></TD> <TD><xsl:value-of select="description"/></TD> <TD>$<xsl:value-of select="price"/></TD> </TR> </xsl:for-each> </xsl:for-each> </TABLE>
The first <xsl:for-each>
selects a set of <invoice>
elements, each of which becomes the context for the "items/item"
query. Each <item>
, in turn, becomes the context for the various queries in the <xsl:value-of>
elements.
Each node selected by <xsl:apply-templates>
is associated with a template and therefore, becomes the current node for the template.
Because each expression defines a new context, a set of nested queries, such as those in the preceding example, produces a set or "path" of context nodes active at one time.
The conditional elements <xsl:if>
and <xsl:when>
do not define a new context for queries within themselves because they do not actually select new nodes. They just test to make sure the nodes are there. In the following example, the <xsl:for-each select="price">
query is relative to <item>
elements, despite the intervening conditional that tests the "qty"
value:
<TABLE> <xsl:for-each select="items/item"> <TR> <TD> <xsl:value-of select="qty"/> </TD> <TD> <xsl:value-of select="description"/> </TD> <TD> $<xsl:value-of select="price"/> </TD> <TD> <!-- 10% volume discount --> <xsl:if test="qty[.>=10]"> <xsl:for-each select="price"> <xsl:value-of select="formatNumber(.*.10, '$#,##0.00')"/> </xsl:for-each> </xsl:if> </TD> <TD STYLE="text-align:right"> <!-- line total --> <xsl:value-of select="formatNumber(user:lineTotal(.), '$#,##0.00')"/> </TD> </TR> </xsl:for-each> </TABLE>
The <xsl:template>
element has a match
attribute that accepts a pattern for the purposes of matching templates to specific elements; there is no fixed context for a query in this case. For more information, see Defining Match Patterns in <xsl:template>.
The preceding code listings are from the Invoice sample. For more information, see Sample XML File for DOM Context.