Suppose i have a template foo
which can output something given a parameter. Now I want to use that output as a parameter to my other template, loop
so I can loop the output a certain number of times. I have tried something along the way of
<xsl:call-template name="loop">
<xsl:with-param name="times" select="someParam"/>
<xsl:with-param name="output">
<xsl:call-template name="foo">
<xsl:with-param name="type" select="something"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
In other words, output
should now contain the output from the call to foo
. Both loop
and foo
work fine independantely but it seems like I can't nest them this way. How should I accomplish this? Thanks in advance.
The problem is in the code you haven't shown to us. This is a correct way to chain/pipe templates, although I would not recommend it (see at the end of this answer),
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:call-template name="loop">
<xsl:with-param name="times" select="3"/>
<xsl:with-param name="output">
<xsl:call-template name="foo">
<xsl:with-param name="pN" select="5"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template name="loop">
<xsl:param name="times" select="1"/>
<xsl:param name="output" select="2"/>
<xsl:choose>
<xsl:when test="not($times > 0)">
<xsl:value-of select="$output"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="loop">
<xsl:with-param name="times" select="$times -1"/>
<xsl:with-param name="output" select="2*$output"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="foo">
<xsl:param name="pN" select="1"/>
<xsl:value-of select="2*$pN"/>
</xsl:template>
</xsl:stylesheet>
when applied on any XML (not used), produces the wanted, correct result:
80
Stylistic recommendation:
Try to avoid chaining templates in this way as this results in unreadable and unmaintainable code.
It is much better to obtain the intermediate results into (properly named) variables. Not only is the code more readable and maintainable this way, but any intermediate result can be re-used multiple times without the need to re-evaluate it.
Here is the same transformation but factored to meet the recommended stylistic requirements:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="vTwice">
<xsl:call-template name="twice">
<xsl:with-param name="pN" select="5"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="loop">
<xsl:with-param name="pTtimes" select="3"/>
<xsl:with-param name="pN" select="$vTwice"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="loop">
<xsl:param name="pTtimes" select="1"/>
<xsl:param name="pN" select="2"/>
<xsl:choose>
<xsl:when test="not($pTtimes > 0)">
<xsl:value-of select="$pN"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="loop">
<xsl:with-param name="pTtimes" select="$pTtimes -1"/>
<xsl:with-param name="pN" select="2*$pN"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="twice">
<xsl:param name="pN" select="1"/>
<xsl:value-of select="2*$pN"/>
</xsl:template>
</xsl:stylesheet>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With