我遇到了一个问题,其中 $current 变量的值以“6E144270003”的形式出现,并且在 Saxon 2.0 处理器中失败,错误为“Cast failed, invalid lexical value - xs:double”。
我无法重现这个问题。
这种转变:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="current" select="'6E144270003'"/>
<xsl:template match="/">
<xsl:if test="number($current) = number($current)">
<xsl:value-of select="$current"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
当使用 SAXON 9.0.0.4 运行时,会产生:
6E144270003
我不确定为什么当它不是数字时会发生这种情况以及如何更正它。基本上如果它不是一个数字我不想输出它
该字符串"6E144270003"
可以在 XPath 2.0 中用作数字,因为在 XPath 2.0 中,所谓的*科学记数法是表示数字的有效方式。
这是一个有趣的示例,其中 XSLT 1.0 和 XSLT 2.0 的行为不同。
更新: OP 表示他想要一个测试来评估false()
字符串是否包含除数字以外的任何内容。
这可以通过正则表达式来最好地实现,甚至:
translate($s, '0123456789', '') eq ''
UPDATE2:OP再次改变了他的问题!!!
对于最新的问题,这里是答案:
使用:
$s castable as xs:decimal
这种转换证明了这种方法的正确性:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:sequence select=
"for $s in ('1234567890',
'123.45',
'123.5',
'-123.45',
'6E144270003'
)
return
if($s castable as xs:decimal)
then $s
else ()
"/>
</xsl:template>
</xsl:stylesheet>
当将此转换应用于任何 XML 文档(未使用)时,将产生正确的结果:
1234567890 123.45 123.5 -123.45