XML Tutorials - Herong's Tutorial Examples - Version 5.04, by Dr. Herong Yang
"apply-templates" - The Child Transformation Call Element
This section describes the 'apply-templates' element, which is used in the content of a 'template' element. The evaluation of an 'apply-templates' element is to perform a child transformation on some selected elements in the source XML document.
"apply-templates": A child element used as part of the content of a "template" element. An "apply-templates" element serves as a child transformation call statement during the process when the transformation engine is evaluating the content of the parent template.
An "apply-templates" element is not declaration element. An "apply-templates" element is an action element. When "apply-templates" element is evaluated, or executed, some elements from source XML document will be selected and transformed. The transformation result become the evaluation result.
The syntax of the "apply-templates" element is shown in 2 examples below:
<xsl:apply-templates select="pattern"/> <xsl:apply-templates/>
where "pattern" is a pattern used to match elements in the source XML document.
If "pattern" matches nothing, the evaluation result is empty.
If "pattern" matches multiple parts, all of the matched parts will be transformed. The evaluation result is the concatenation of transformation result texts of all matched elements.
If attribute "select" is not specified, the default selection pattern will be used. The default selection pattern will match all child elements of current context.
For example, the first template in following stylesheet is defined with an "apply-templates" element as part of the content mixed with literal texts.
<xsl:template match="p"> Text part 1. <xsl:apply-templates select="c"/> Text part 2. </xsl:template> <xsl:template match="c"> Literal text for "c". </xsl:template>
The content evaluation logic will be:
To test the above stylesheet, I wrote this source XML document:
<p a="v"> Some text with a <c>child element</c>. </p>
If the first template of the above stylesheet is applied. the transformation engine will match the element "p" in the source XML document with the first template, and insert template's content into the result document:
Text part 1. Literal text for "c". Text part 2.
Last update: 2014.
Table of Contents