Xslt 在XSL中测试xs:decimal的正确方法是什么?
我试图根据传入的数据显示不同的信息。如果是整数,我只想显示数字,如果是十进制,我想使用0.00#模式。是的,我知道,有点混乱,但这是开发规范:> 对于这个特定的部分,我有下面的XSL,但是我看不到要通过的XSL:when错误消息 “应为表达式结尾,已找到 “浇注料”。编号(SAVG)-->浇注料 0"> &;书信电报;1. 不适用 我试着四处寻找答案,我试过“实例”,我试过使用xsl:if等,但我似乎无法实现这一点。任何帮助都将不胜感激 谢谢 来自评论:Xslt 在XSL中测试xs:decimal的正确方法是什么?,xslt,Xslt,我试图根据传入的数据显示不同的信息。如果是整数,我只想显示数字,如果是十进制,我想使用0.00#模式。是的,我知道,有点混乱,但这是开发规范:> 对于这个特定的部分,我有下面的XSL,但是我看不到要通过的XSL:when错误消息 “应为表达式结尾,已找到 “浇注料”。编号(SAVG)-->浇注料 0"> &;书信电报;1. 不适用 我试着四处寻找答案,我试过“实例”,我试过使用xsl:if等,但我似乎无法实现这一点。任何帮助都将不胜感激 谢谢 来自评论: <xsl:styleshe
<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>
是的,我们正在使用1.0。对不起,我很抱歉
XSL处理的新手,我该怎么做
粘合XSL和输入以生成
html
此XSLT 1.0样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="test">
<xsl:choose>
<!-- Not number or number less than zero -->
<xsl:when test="0 > SVGA or number(SVGA) != SVGA">
<xsl:text>N/A</xsl:text>
</xsl:when>
<!-- Number zero -->
<xsl:when test="SVGA = 0">
<xsl:text><1</xsl:text>
</xsl:when>
<!-- Integer number -->
<xsl:when test="floor(SVGA) = SVGA">
<xsl:value-of select="format-number(SVGA,'###,###,##0.###')"/>
</xsl:when>
<!-- Double number -->
<xsl:otherwise>
<xsl:value-of select="format-number(SVGA,'###,###,##0.00#')"/>
</xsl:otherwise>
</xsl:choose>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
编辑3:更好的测试顺序(加上Dimitre的表达式)、更好的测试用例、更接近提问者的输入样本。I.XSLT 1.0:
<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>
3 is xs:integer
3.14 is 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 $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>
3
3.14
XSLT1.0使用的XPath1.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>
3 is xs:integer
3.14 is 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 $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>
3
3.14
有一种方法可以做到这一点:
<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>
或者,按照您的示例使用format-number()
函数:
<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>
3 is xs:integer
3.14 is 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 $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>
3
3.14
这是XSLT2.0。如果您是使用XSLT1.0处理器运行此程序,请这样说。好问题(+1)。请参阅我的答案,以获得简短而完整的XSLT 2.0解决方案。我在答案中添加了XSLT 1.0解决方案。是的,我们使用的是1.0。很抱歉,我不熟悉XSL处理,如何粘合XSL和输入以生成html?@PHenry:请参阅我的XSLT1.0编辑解决方案。我不明白你所说的用胶水(…)来生成html是什么意思。请发布输入样本和所需输出。重新粘合,哦,对不起,我的意思是我需要对这些行做什么?我认为XSL会进入XSL文件,html部分会进入html文件,但我不确定如何从html引用XSL。如果这完全是胡说八道,我道歉,我正在努力学习。@Phery:我想你之前已经运行过一次转换。。。您应该将
xsl:choose
指令替换为您的Dimitre或我发布的指令(适当时替换,即
替换为SAVG
)@Dimitre:+1我更喜欢XSLT 2.0。@Dimitre:OP评论他/她需要XSLT 1.0, now@Alejandro:我还添加了一个XSLT 1.0解决方案。@Dimitre:如果可以,我将+1用于fn:floor
数字测试。@TheManWholdtheWorld,如果您可以使用XSLT 2.0(XPath 2.0),在您的示例中:100.00 xs:integer
实例的计算结果为false()
,这正是您所需要的。整数的经典数学定义可以使用:100.00 castable as xs:integer
——其计算结果为true()
。无论如何,XPath 1.0中的实际数字类型总是double
——XPath 1.0中没有xs:decimal
或xs:integer
类型。这意味着任何这样的表达式只能帮助我们确定给定的双精度值是否恰好是整数的值。