Templates

   

Template Content

The content of the template specifies what output it will generate. Typically, the template contains a mixture of text, XML elements, and XSL instructions.

Two styles for writing templates exist. A template can be input directed or output controlled. The two modes are not exclusive; in fact, many style sheets effectively combine the two.

Input Directed

These templates are guided by the input document. Essentially, the processor recursively walks the input document, selects a template that matches the current node, and applies it.

The xsl:apply-templates element moves one level down in the input document, kicking in the recursion:

 <xsl:template match="Title">       <H1><xsl:apply-templates/></H1>    </xsl:template> 

The select and mode attributes control how the processor walks the input tree. For more information about mode , see the section Priority and Mode earlier in the chapter. select is an XPath that controls which nodes the processor visits next :

 <xsl:template match="Body">       <H1><xsl:apply-templates select="Section/Title"/></H1>    </xsl:template> 

Output Controlled

When the output is regular and well defined, it is more efficient to use xsl:for-each in combination with xsl:value-of elements:

 <xsl:template match="Body">       <xsl:for-each select="Title">          <H1><xsl:value-of select="."/></H1>       </xsl:for-each>    </xsl:template> 

XML Elements

To generate XML elements in the output document, insert them in a template:

 <xsl:template match="Date">       <PODate><xsl:apply-templates/></PODate>    </xsl:template> 

Alternatively, a style sheet can compute elements with xsl:element , which can compute the element name from the style sheet:

 <xsl:template match="Field">       <xsl:element name="{ @id}">          <xsl:apply-templates/>       </xsl:element>    </xsl:template> 

Tip

The xsl: namespace-alias enables you to transform namespaces between the input and output documents. It is typically used for style sheets that need to output elements in the XSL namespace:

 <xsl:stylesheet          version="1.0"          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"          xmlns:alias="http://www.psol.com/XSL/Alias">        <xsl:namespace-alias stylesheet-prefix="alias"                             result-prefix="xsl"/>        <!-- templates deleted-->        </xsl:stylesheet> 

XML Attributes

Likewise, to generate attributes in the output document, insert them in the template:

 <xsl:template match="Date">       <PODate format="ISO"><xsl:apply-templates/></PODate>    </xsl:template> 

If the value of the attribute is an XPath, you must enclose it in curly brackets:

 <xsl:template match="Date">       <PODate format="{ Format}"><xsl:apply-templates/></PODate>    </xsl:template> 

On the other hand, a style sheet can compute elements with xsl:attribute , which computes the attribute name or value from the style sheet:

 <xsl:template match="Date">       <PODate><xsl:attribute name="format"/>          <xsl:call-template name="get-format"/>       </xsl:attribute></PODate>    </xsl:template> 

Text

In most cases, to output text, you type it in the template. However, to gain better control on how the processor interprets whitespaces, you should mark up the text with an xsl:text element:

 <xsl:template match="Date">       <xsl:text>Date: </xsl:text>       <xsl:value-of select=".">    </xsl:template> 

Tip

To insert entities in the output ”particularly HTML entities such as &nbsp; ”write the following:

 <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text> 

   


Applied XML Solutions
Applied XML Solutions
ISBN: 0672320541
EAN: 2147483647
Year: 1999
Pages: 142

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