一、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>
Do note:要测试数值是否为整数,我们使用以下测试:
floor($someNum) = $someNum
这是执行此操作的一种方法:
<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', '
')
else if($num instance of xs:decimal)
then ($num, ' is xs:decimal', '
')
else ($num, ' is something else', '
')
"/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于任何 XML 文档(未使用)时,会生成所需的正确结果:
3 is xs:integer
3.14 is xs:decimal
或者,使用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.###'), '
')
else if($num instance of xs:decimal)
then (format-number($num, '###,###,##0.00#'), '
')
else ()
"/>
</xsl:template>
</xsl:stylesheet>
produces:
3
3.14