XSLT 编号测试

发布于 2024-09-27 04:35:10 字数 403 浏览 1 评论 0原文

我遇到一个问题,$current 变量的值是“6E144270003”。我最初的目标是只测试一个数字,但“6E144270003”通过了 number() 测试,因为它是一个有效的“科学记数法”(正如这里向我指出的那样)。

我需要一个有效的测试来允许仅包含整数(可以包括小数和减号)的数据等于 true,而任何其他数据等于 false。

应通:1234567890
应通过:123.45
应通过:123.5
应通过:-123.45

 <xsl:if test="number($current) = number($current)">  
    <xsl:value-of select="$current"/>   
 </xsl:if>  

I am having a problem where the value of the $current variable came in as '6E144270003'. My original goal was to just test for a number, but '6E144270003' passed the number() test because it is a valid 'scientific notation' (as was pointed out to me here).

I need a valid test to allow data containing only integers (can include the decimal and minus sign) to equate to true and any other data to equate to false.

Should pass: 1234567890
Should pass: 123.45
Should pass: 123.5
Should pass: -123.45

 <xsl:if test="number($current) = number($current)">  
    <xsl:value-of select="$current"/>   
 </xsl:if>  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡渣熟男 2024-10-04 04:35:10

我遇到一个问题,值
$current 变量的值是
'6E144270003' 并且失败了
Saxon 2.0 处理器,错误为
'转换失败,词法值无效 -
xs:双'。

我无法重现这个问题。

此转换

<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

I am having a problem where the value
of the $current variable came in as
'6E144270003' and it failed in the
Saxon 2.0 processor with an error of
'Cast failed, invalid lexical value -
xs:double'.

I cannot repro this problem.

This transformation:

<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>

when run with SAXON 9.0.0.4, produces:

6E144270003

I'm unsure why this happened when it
is not a number and also how to
correct it. Basically if it is not a
number I don't want to output it

The string "6E144270003" can be used as a number in XPath 2.0, because in XPath 2.0 the so called *scientific notation` is a valid way to represent a number.

This is one interesting example where the behavior of XSLT 1.0 and XSLT 2.0 is different.

UPDATE: The OP has indicated that he wants a test that will evaluate to false() if the string contains anything but digits.

This can be achieved best by a regular expression, or even:

translate($s, '0123456789', '') eq ''

UPDATE2: The OP changed his question again!!!

For the latest question here is the answer:

Use:

$s castable as xs:decimal

This transformation demonstrates the correctness of this method:

<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>

when this transformation is applied on any XML document (not used), the correct result is produced:

1234567890 123.45 123.5 -123.45
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文