Subject: Understanding named templates and their parameters
From: Michel Goossens <Michel.Goossens@xxxxxxx>
Date: Sat, 24 Apr 1999 20:17:40 +0200 (METDST)
|
With the Dec. XSL spec I was using macros, whose functionality now seems
to be replaced by named templates and parameters.
I thus wanted to try and understand how to use them by translating
something which worked w/o problems with the previous draft.
I have a little file texmplatest.xml, which contains:
<invitation>
<to>Anna, Bernard, Didier, Johanna</to>
</invitation>
I want this to turn into a list with an identifier, etc. using the
following XSL file (I might not generate completely correct new-draft
XSL formatting objects, but never mind).
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"
xmlns:fo="http://www.w3.org/XSL/Format/1.0"
result-ns="fo"
default-space="">
<xsl:template match='/'>
<fo:page-sequence>
<xsl:apply-templates/>
</fo:page-sequence>
</xsl:template>
<xsl:template match="invitation">
<fo:list-block>
<fo:list-item id="listto">
<fo:list-item-label font-style="italic">
<xsl:text>To:</xsl:text>
</fo:list-item-label>
<fo:list-item-body>
<xsl:value-of select="to"/>
</fo:list-item-body>
</fo:list-item>
</fo:list-block>
</xsl:template>
</xsl:stylesheet>
which gives as output with xt what I want:
<fo:page-sequence
xmlns:fo="http://www.w3.org/XSL/Format/1.0">
<fo:list-block>
<fo:list-item id="listto">
<fo:list-item-label font-style="italic">To:</fo:list-item-label>
<fo:list-item-body>Anna, Bernard, Didier, Johanna</fo:list-item-body>
</fo:list-item>
</fo:list-block>
</fo:page-sequence>
However, now consider the following XSL file, where I try to achieve
the same result with a named template and parameters:
<xsl:template name="listitem">
<xsl:param-variable name="labeltext">labeltext</xsl:param-variable>
<xsl:param-variable name="itemid">itemid</xsl:param-variable>
<xsl:param-variable name="itemtext">itemtext</xsl:param-variable>
<fo:list-item id="{$itemid}">
<fo:list-item-label font-style="italic">
$labeltext<xsl:text>:</xsl:text>
</fo:list-item-label>
<fo:list-item-body>
<xsl:value-of select="$itemtext"/>
</fo:list-item-body>
</fo:list-item>
</xsl:template>
<xsl:template match='/'>
<fo:page-sequence>
<xsl:apply-templates/>
</fo:page-sequence>
</xsl:template>
<xsl:template match="invitation">
<fo:list-block>
<xsl:call-template name="listitem">
<xsl:param name="labeltext">To</xsl:param>
<xsl:param name="itemid">listto</xsl:param>
<xsl:param name="itemtext">to</xsl:param>
</xsl:call-template>
</fo:list-block>
</xsl:template>
</xsl:stylesheet>
When I run this with xt, then I get the following:
<fo:page-sequence
xmlns:fo="http://www.w3.org/XSL/Format/1.0">
<fo:list-block>
<fo:list-item id="listto">
<fo:list-item-label font-style="italic">
$labeltext:</fo:list-item-label>
<fo:list-item-body>to</fo:list-item-body>
</fo:list-item>
</fo:list-block>
</fo:page-sequence>
So the $labeltext parameter was not substituted (putting {} around
it does not work) and the XSL instruction
<xsl:value-of select="$itemtext"/> using the parameter $itemtext
was not "executed", although from somewhere the value "to"
seems to have appeared. What am I missing? How are parameters substituted
inside named templates?
Thanks for your help. Michel Goossens
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|