|
XSL - Declaring and Applying Templates
Part:
1
2
3
4
This tutorial describes:
The "stylesheet" Element
The "template" Element
The "apply-templates" Element
How XSL Transformation Gets Started?
Default Transformation Template
Transformation Chains
Since XSL is a language based on XML, so all XSL statements are written as XML elements.
In this note, we will learn and play with three basic XSL elements:
- "stylesheet": Desclaring an XSL style sheet.
- "template": Declaring an XSL transformation template.
- "apply-templates": Requesting to apply transformation templates.
With them, we can control the XSL processor:
- To transform a named element by replacing it with a given text string.
- To invoke a transformation of a child element within the transformation
of the parent element.
The 'stylesheet' Element
stylesheet: The root element of an XSL file, serving as the XSL declaration
statement. It specifies the XSL version number,
and associates the XSL namespace to a name prefix.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
XSL_elements
</xsl:stylesheet>
The 'template' Element
template: An XSL element, serving as the template declaration statement.
It defines the replacement content for elements in the XML source files
that matches the specified pattern.
<xsl:template match="pattern">
replacement_content
</xsl:template>
where "pattern" is a pattern used to match element names in the source XML file,
and "replace_content" is the output to be inserted in the same place where
the matching elements are.
If an element in the source XML file is matched, the entire
element will be replaced, starting from "<" of the starting tag to ">" of the ending
tag. By this definition, the replacement includes the element name, all attributes,
text content, and all sub-elements.
Example, we have the following element in the source XML file:
<p a="v">
Some text with a <c>child element</c>.
</p>
and the following template in the XSL file:
<xsl:template match="p">
Replacement text for the element "p" in the source XML file.
</xsl:template>
the output will be:
Replacement text for the element "p" in the source XML file.
because the tags, attribute, text content, and child element are all replaced by the
text specified in the template statement.
"replacement_content" in the "template" element contains a mixture of:
- #PCDATA: Text string with entities.
- Non-XSL elements: Regular XML elements.
- XSL elements: XSL elements that produce output text, or specifying additional
transformation rules.
Example:
<xsl:template match="p">
Replacement text for the element "p" in the source XML file.
</xsl:template>
If this template is used on a "p" element in the XML source file, that element will be
replaced entirely by the text specified in the template.
Example:
<xsl:template match="p">
Replacement text with <b>one XML element</b> for the element "p"
in the source XML file.
</xsl:template>
This example is the same as the example 1, except that we have a non XSL element in
the replacement text. No big deal. That non XSL element will be part of the result
file produced by the transformation. However, since XSL file is also an XML file,
the replacement text 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>.
(Continued on next part...)
Part:
1
2
3
4
|