Xslt 将字符串传递到需要节点的模板参数中?
所以, 我有一个XSLT模板,它需要一个节点集作为参数,并使用它作为显示文本。但是,有时此节点在XML中为空,我希望传递默认显示文本,而不是不显示的显示文本: 作品:Xslt 将字符串传递到需要节点的模板参数中?,xslt,xpath,Xslt,Xpath,所以, 我有一个XSLT模板,它需要一个节点集作为参数,并使用它作为显示文本。但是,有时此节点在XML中为空,我希望传递默认显示文本,而不是不显示的显示文本: 作品: <xsl:call-template name="myTemplate"> <xsl:with-param name="parm1" select="//element"> </xsl:call-template> 不起作用: <xsl:variable name="disp
<xsl:call-template name="myTemplate">
<xsl:with-param name="parm1" select="//element">
</xsl:call-template>
不起作用:
<xsl:variable name="dispText">
<xsl:choose>
<xsl:when test="string-length(//element) = 0">
<xsl:value-of select="'Default Text'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="//element" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:call-template name="myTemplate">
<xsl:with-param name="parm1" select="$dispText">
</xsl:call-template>
关于我如何做到这一点,有什么想法吗?我尝试过各种各样的事情,但都不走运:(
看起来我需要做的就是用我想要的显示文本创建一个新节点,但我不知道这是否可行
谢谢我猜//元素是一个节点集,对其使用string-length()可能无效。请先尝试将其转换为string()?在模板中实现默认处理,因为它就属于该模板。调用方应该是一致的,并且不会对模板行为产生副作用(即,您不能“忘记”传递默认值)
string-length()
在节点集上有效。它测量集合中第一个节点的字符串表示形式。
<xsl:template name="myTemplate">
<xsl:param name="parm1" /><!-- node set expected! -->
<!-- actual value or default -->
<xsl:variable name="value1">
<xsl:choose>
<xsl:when test="not($parm1 = '')">
<xsl:value-of select="$parm1" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$default1" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- work with $value1 from this point on -->
</xsl:template>