A346 - XSL Templates |
Modified: |
Templates are modules of XSL instructions that are called from other templates. Templates promote modular designs and reuse in much the same way of functions. The following reviews templates and presents the use of template parameters to produce more general modules.
Templates are modules that can be applied or called.
apply
call
The example shows the XSL at left, the resulting HTML in the middle and the rendering at right.
RowColumn.htm
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
<table border="1">
<xsl:call-template name="RowColumn"/>
<xsl:call-template name="RowColumn"/>
<xsl:call-template name="RowColumn"/>
</table>
</xsl:template>
<xsl:template name="RowColumn">
<tr><td>Row</td></tr>
</xsl:template>
</xsl:stylesheet>
|
HTML produced <?xml version="1.0" encoding="UTF-16" ?> <table border="1"> <tr><td>Row</td></tr> <tr><td>Row</td></tr> <tr><td>Row</td></tr> </table>
|
Browser
|
RowColumn.xml
<?xml-stylesheet type="text/xsl"
href="RowColumn.htm"?>
<top>
</top>
|
Template parameters are text passed from the calling template to the called template. Passing 'Second' as the value of the Row parameter to template RowColumn:
<xsl:call-template name="RowColumn">
<xsl:with-param name="Row" select=" 'Second' " />
</xsl:call-template>
Receiving 'Second' from the above example to Row parameter:
<xsl:template name="RowColumn">
<xsl:param name="Row" />
<tr><td>
<xsl:value-of select="$Row" />
</td></tr>
</xsl:template>
The value of the parameter is available by 2 different methods dependent upon where used. For the Row parameter:
If one doesn't work, try the other.
In the following example, the text 'First', 'Second', and 'Third' is passed through the parameter to template RowColumn named Row.
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:template match="/">
<table border="1">
<xsl:call-template name="RowColumn">
<xsl:with-param name="Row" select=" 'First' " />
</xsl:call-template>
<xsl:call-template name="RowColumn">
<xsl:with-param name="Row" select=" 'Second' " />
</xsl:call-template>
<xsl:call-template name="RowColumn">
<xsl:with-param name="Row" select="3" />
</xsl:call-template>
</table>
</xsl:template>
<xsl:template name="RowColumn">
<xsl:param name="Row" />
<tr>
<td id="{$Row}">
<xsl:value-of select="$Row" />
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
|
<?xml version="1.0" encoding="UTF-16" ?> <table border="1"> <tr> <td id="First">First</td> </tr> <tr> <td id="Second">Second</td> </tr> <tr> <td id="3">3</td> </tr> </table>
|
|
We need to see the HTML produced by the style sheet for debugging. The browser source view only displays the XML file, not useful when debugging style sheets.
Exercise 1 - Debugging
|