XML Tutorials - Herong's Tutorial Examples - Version 5.02, by Dr. Herong Yang
"template" - The Template Declaration Element
This section describes the 'template' element, which declares a transformation template that can be used to generate output text when applied to any matching source elements.
"template": A top-level element inside the "stylesheet" element in an XSLT stylesheet file. A "template" element serves as the template declaration statement.
A "template" element defines an XML transformation template with an XPath expression to match elements in the source XML document. A "template" element also provides result text for the resulting document.
When the transformation engine is processing a source element that matches the XPath expression of a template, the result text of the template will be inserted into the resulting document.
The syntax of the "template" element is:
<xsl:template match="pattern"> content </xsl:template>
"pattern" in a template element is an XPath expression used to match parts in the source XML file. For example, match="p" provides a pattern that matches any element with element name of "p" in the current context.
"content" in a template element is a string expression to be evaluated as the result text for the resulting document. "content" can be a mixture of:
If an element in the source XML file matches a template in stylesheet, the entire source element will be considered as processed as soon as the result text is generated. The entire source element means everything starting from "<" of the starting tag to ">" of the ending tag, including the element name, all attributes, text content, and all sub-elements.
For example, the following template is defined with pure #PCDATA as its content:
<xsl:template match="p"> Literal text as template content. </xsl:template>
To test the above template, I wrote this source XML document:
<p a="v"> Some text with a <c>child element</c>. </p>
If the above template is applied, the transformation engine will match the element "p" in the source XML document with the defined template, and insert template's content into the result document:
Literal text as template content.
When this is done, the transformation engine will skip the child element "c" and the attribute "a".
Last update: 2014.
Table of Contents