Xslt 调用XSL模板时的可选参数
有没有方法使用可选参数调用XSL模板 例如:Xslt 调用XSL模板时的可选参数,xslt,Xslt,有没有方法使用可选参数调用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: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>
…可能更多的参数。。。
这段代码会给我一个错误“Expression error:variable'baz'not found.”是否可以省略“baz”声明
谢谢,,
Henry您使用的
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>
…可能更多的参数。。。
Param获取使用与xsl:Param
语句名称匹配的xsl:Param
传递的参数值。如果未提供任何值,则采用select
属性full XPath的值
有关更多详细信息,请访问。如果不传递参数,将使用param元素的select部分中的值 由于变量或参数$baz尚不存在,因此出现错误。它必须在顶层定义,才能在您的示例中工作,这不是您想要的 另外,如果要将文本值传递给模板,那么应该像这样传递它
<xsl:call-template name="test">
<xsl:with-param name="foo">fooValue</xsl:with-param>
食品价值
就个人而言,我更喜欢做以下工作:
<xsl:call-template name="test">
<xsl:with-param name="foo">
<xsl:text>fooValue</xsl:text>
</xsl:with-param>
食品价值
我喜欢显式地使用文本,这样我就可以在XSL上使用XPath进行搜索。在对我没有编写或不记得的XSL进行分析时,它已经派上了很多用场。如果不需要它来增加可读性,请不要使用
这非常有效:
<xsl:template name="inner">
<xsl:value-of select="$message" />
</xsl:template>
<xsl:template name="outer">
<xsl:call-template name="inner">
<xsl:with-param name="message" select="'Welcome'" />
</xsl:call-template>
</xsl:template>