如果值是数字,我希望变量具有元素的值,但如果不是数字,那么我希望变量具有值 0
。
换句话说,XSLT 中是否存在与以下内容等效的简单内容?
var foobar = is_numeric(element value) ? element value : 0
或者你会怎么写这个?
<xsl:variable name="foobar" select=" ? " />
最佳答案
XPath 1.0:
<xsl:variable name="foobar">
<xsl:choose>
<xsl:when test="number($value) = number($value)">
<xsl:value-of select="$value"/>
</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
引用这个聪明的number($value) = number($value)
数字测试:Dimitre Novatchev's answer至 "Xpath test if is number" question .
关于variables - XSLT 中的条件变量选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19139584/