我遇到了值的问题
的 $current 变量作为
'6E144270003',它在
Saxon 2.0 处理器错误为
'转换失败,无效的词法值 -
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
我不确定为什么会发生这种情况
不是数字以及如何
纠正它。基本上如果不是
number 我不想输出它
字符串"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