在XSL中测试xs:decimal的正确方法是什么?

PHe*_*nry 6 xslt

我正在尝试根据传入的数据显示不同的信息.如果它是一个整数,我想只显示数字,如果它是小数,我想使用0.00#pattern.雅,我知道,有点混乱,但那是开发规范.:>

我有这个特定部分的以下XSL,但我看不到超过xsl:when错误消息

"预期结束表达,发现'castable'.number(SAVG) - > castable < - as xs:decimal"

<xsl:choose>
    <xsl:when test="number(SAVG) > 0">
        <xsl:choose>
            <xsl:when test="number(SAVG) castable as xs:decimal">
                <xsl:value-of select="format-number(SAVG, '###,###,##0.00#')"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="format-number(SAVG, '###,###,##0.###')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:when>
    <xsl:when test="number(SAVG) = 0">
        <xsl:text disable-output-escaping="yes">&amp;lt;</xsl:text>1
    </xsl:when>
    <xsl:otherwise>N/A</xsl:otherwise>
</xsl:choose>
Run Code Online (Sandbox Code Playgroud)

我试着寻找/寻找答案,我尝试了"实例",我尝试过使用xsl:if等,但我似乎无法让它工作.任何帮助将不胜感激.

谢谢.

来自评论:

是的,我们使用的是1.0.对不起,我是XSL处理的新手,如何粘合你的XSL和输入来生成html?

Dim*_*hev 6

I. XSLT 1.0:

在XSLT 1.0使用的XPath 1.0数据模型中没有xs:integer和xs:decimal.

以下是您可以使用的代码段:

    <xsl:choose> 
        <xsl:when test="not(floor(SAVG) = SAVG)"> 
            <xsl:value-of select="format-number(SAVG, '###,###,##0.00#')"/> 
        </xsl:when> 
        <xsl:otherwise> <!-- Integer value -->
            <xsl:value-of select="SAVG"/> 
        </xsl:otherwise> 
    </xsl:choose> 
Run Code Online (Sandbox Code Playgroud)

注意:要测试数值是否为整数,我们使用以下测试:

 floor($someNum) = $someNum
Run Code Online (Sandbox Code Playgroud)

这是一种方法:

<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 $num in (3, 3.14)
     return
       if($num instance of xs:integer)
         then ($num, ' is xs:integer', '&#xA;')
         else if($num instance of xs:decimal)
           then ($num, ' is xs:decimal', '&#xA;')
           else ($num, ' is something else', '&#xA;')
   "/>
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当此转换应用于任何XML文档(未使用)时,将生成所需的正确结果:

3  is xs:integer 
3.14  is xs:decimal 
Run Code Online (Sandbox Code Playgroud)

或者,format-number()根据您的示例使用该功能:

<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 $num in (3, 3.14)
     return
       if($num instance of xs:integer)
         then (format-number($num, '###,###,##0.###'), '&#xA;')
         else if($num instance of xs:decimal)
           then (format-number($num, '###,###,##0.00#'), '&#xA;')
           else ()
   "/>
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

产生:

3 
3.14 
Run Code Online (Sandbox Code Playgroud)