我已尽量将问题缩小到尽可能小的测试用例。
我目前有以下样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template name="set-attrib">
<xsl:param name="val" select="3"/>
<xsl:attribute name="value">
<xsl:value-of select="$val" />
</xsl:attribute>
</xsl:template>
<xsl:template match='@value[parent::element]'>
<xsl:call-template name="set-attrib">
<with-param name="val">2</with-param>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
并将其应用于以下XML:
<?xml version="1.0"?>
<element value="0"/>
我得到了输出:
<?xml version="1.0"?>
<element value="3"/>
而不是我期望的那样:
<?xml version="1.0"?>
<element value="2"/>
模板set-attrib
似乎忽略了val
中设置的参数call-template
。
我在网上挖掘了各种想法,并尝试了不同的排列(例如在select="2"
中使用with-param
,或将xsl:param
的默认值移到<!-- Content: template -->
中,而不是在select
属性中,没有区别。
我似乎能够让它发挥作用的唯一方法是在set-attrib
模板中对参数进行硬编码或完全内联call-template
。
我看不出这与其他示例代码有什么不同;我错过了一些明显的东西吗?
答案 0 :(得分:0)
<with-param name="val">2</with-param>
需要<xsl:with-param name="val">2</with-param>
。有了这个改变,http://xsltransform.net/bFDb2BS就可以了。