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

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

To test the above template, I wrote this source XML document:

<p a="v">
 Some text with a <c>child element</c>.

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

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 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

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

 Java Implementation of XSLT

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

 Control and Generate XML Element in the Result

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML 1.1 Changes and Parsing Examples

 Outdated Tutorials


 PDF Printing Version