XML Tutorials - Herong's Tutorial Examples - Version 5.05, by Dr. Herong Yang
Including Literal XML Elements in Templates
This section describes how to include literal XML elements in template as part template's content. A literal XML element is a non-XSLT element.
As mentioned in the previous section, the content in a template declaration statement can contain #PCDATA (literal text with XML entities).
The content in a template declaration statement can also contain literal XML elements (non-XSLT elements).
For example, the following template is defined with a pure literal XML element as its content:
<xsl:template match="p"> <b>Literal XML element as template content.</b> </xsl:template>
In this example, the template contains a single literal XML element as its content. The transformation engine should take it as is with no changes.
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:
<b>Literal XML element as template content.</b>
When this is done, the transformation engine will skip the child element "c" and the attribute "a".
Since XSLT stylesheet is also an XML file, literal XML elements included in templates must follow XML rules. For example, the following template statement make trouble for the transformation process:
<xsl:template match="p"> Replacement text with <b>one XML element</B> for the element "p" in the source XML file. </xsl:template>
Why? The ending tag </B> doesn't match the starting tag <b>.
Last update: 2014.
Table of Contents