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>.

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 XML-JSON Document Conversion

 DOM (Document Object Model) Programming Interface

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating XML Documents Against Specified XML Schemas

 XSL (Extensible Stylesheet Language) Introduction

 Java Implementation of XSLT

XSLT (XSL Transformations) Introduction

 What Is XSLT (XSL Transformations)

 "stylesheet" - The Stylesheet Declaration Element

 "template" - The Template Declaration Element

Including Literal XML Elements in Templates

 "apply-templates" - The Child Transformation Call Element

 How the Transformation Process Gets Started

 Default Transformation Template

 XML Element Transformation Chains

 XML Element Transformation Chains - Complex Example

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

 Control and Generate XML Element in the Result

 PHP Extensions for XML Manipulation

 Processing XML with Python Scripts

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML Plugin Packages for Atom Editor

 XML 1.1 Changes and Parsing Examples

 Archived Tutorials

 References

 Full Version in PDF/EPUB