XST - 使用调用模板的输出作为返回值 [英] XST - using output from call-template as return value
问题描述
假设我有一个模板 foo
可以输出给定参数的东西.现在我想将该输出用作我的另一个模板 loop
的参数,以便我可以循环输出一定次数.我一直在尝试一些东西
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>
换句话说,output
现在应该包含调用 foo
的输出.loop
和 foo
都可以独立工作,但似乎我不能以这种方式嵌套它们.我应该如何做到这一点?提前致谢.
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.
推荐答案
问题在于您没有向我们展示的代码.这是链接/管道模板的正确方法,尽管我不推荐它(见本答案末尾),
这种转变:
<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>
应用于任何 XML(未使用)时,产生所需的正确结果:
80
风格推荐:
尽量避免以这种方式链接模板,因为这会导致代码不可读和不可维护.
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.
这是相同的转换,但考虑到了建议的风格要求:
<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>
这篇关于XST - 使用调用模板的输出作为返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!