有没有办法调用带有可选参数的XSL模板?
例如:
<xsl:call-template name="test">
<xsl:with-param name="foo" select="'fooValue'" />
<xsl:with-param name="bar" select="'barValue'" />
</xsl:call-template>
以及生成的模板定义:
<xsl:template name="foo">
<xsl:param name="foo" select="$foo" />
<xsl:param name="bar" select="$bar" />
<xsl:param name="baz" select="$baz" />
...possibly more params...
</xsl:template>
这段代码将给我一个错误“表达式错误:变量'baz‘找不到。”可以省略"baz“声明吗?
谢谢你,亨利
发布于 2009-05-06 17:33:52
您错误地使用了xsl:param
语法。
改为执行以下操作:
<xsl:template name="foo">
<xsl:param name="foo" />
<xsl:param name="bar" />
<xsl:param name="baz" select="DEFAULT_VALUE" />
...possibly more params...
</xsl:template>
参数采用使用与xsl:param
语句的名称匹配的xsl:with-param
传递的参数值。如果未提供,则采用select
属性full XPath的值。
更多细节可以在W3School's entry on param上找到。
发布于 2009-05-06 22:07:37
就我个人而言,我更喜欢做以下事情:
<xsl:call-template name="test">
<xsl:with-param name="foo">
<xsl:text>fooValue</xsl:text>
</xsl:with-param>
我喜欢显式地使用文本,这样我就可以在我的XSL上使用XPath进行搜索。在对我没有编写或不记得的XSL进行分析时,它多次派上用场。
发布于 2009-05-06 17:40:50
如果不传递参数,将使用param元素的select部分中的值。
您收到错误,因为变量或参数$baz尚不存在。它必须在顶层定义,才能在您的示例中工作,这并不是您想要的。
同样,如果你要传递一个文字值给一个模板,那么你应该像这样传递它。
<xsl:call-template name="test">
<xsl:with-param name="foo">fooValue</xsl:with-param>
https://stackoverflow.com/questions/830780
复制相似问题