8.4 Templates and Template Rules


To control what output is created from what input, you add template rules to the XSLT stylesheet. Each template rule is represented by an xsl:template element. This element has a match attribute that contains a pattern identifying the input it matches; it also contains a template that is instantiated and output when the pattern is matched. The terminology is a little tricky here: the xsl:template element is a template rule that contains a template. An xsl:template element is not itself the template.

The simplest match pattern is an element name . Thus, this template rule says that every time a person element is seen, the stylesheet processor should emit the text "A Person":

 <xsl:template match="person">A Person</xsl:template> 

Example 8-4 is a complete stylesheet that uses this template rule.

Example 8-4. An XSLT stylesheet with a match pattern
 <?xml version="1.0"?> <xsl:stylesheet version="1.0"                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">         <xsl:template match="person">A Person</xsl:template>       </xsl:stylesheet> 

Applying this stylesheet to the document in Example 8-1 produces this output:

 <?xml version="1.0" encoding="utf-8"?>        A Person        A Person 

There were two person elements in the input document. Each time the processor saw one, it emitted the text "A Person". The whitespace outside the person elements was preserved, but everything inside the person elements was replaced by the contents of the template rule, which is called the template .

The text "A Person" is called literal data characters , which is a fancy way of saying plain text that is copied from the stylesheet into the output document. A template may also contain literal result elements, i.e., markup that is copied from the stylesheet to the output document. For instance, Example 8-5 wraps the text "A Person" in between <p> and </p> tags.

Example 8-5. A simple XSLT stylesheet with literal result elements
 <?xml version="1.0"?> <xsl:stylesheet version="1.0"                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">         <xsl:template match="person">     <p>A Person</p>   </xsl:template>       </xsl:stylesheet> 

The output from this stylesheet is:

 <?xml version="1.0" encoding="utf-8"?>         <p>A Person</p>         <p>A Person</p> 

The <p> and </p> tags were copied from the input to the output. The only major restriction on the markup you may output is that it must be well-formed XML because the stylesheet must be well- formed XML. For instance, you cannot write a template rule like this:

 <xsl:template match="person">   A Person<p> </xsl:template> 

Here the <p> start-tag has no matching end-tag; therefore, the stylesheet is malformed . Any other markup in the XSLT stylesheet must be similarly well-formed. Empty-element tags must end with /> , attribute values must be quoted, less-than signs must be escaped as &lt; , all entity references must be declared in a DTD except for the five predefined ones, and so forth. XSLT has no exceptions to the rules of well- formedness .

XML in a Nutshell
XML in a Nutshell, Third Edition
ISBN: 0596007647
EAN: 2147483647
Year: 2003
Pages: 232

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net