Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 在XSLT中用逗号作为小数分隔符对数字求和?_Xml_Xslt_Sum_Decimalformat - Fatal编程技术网

Xml 在XSLT中用逗号作为小数分隔符对数字求和?

Xml 在XSLT中用逗号作为小数分隔符对数字求和?,xml,xslt,sum,decimalformat,Xml,Xslt,Sum,Decimalformat,我有一个XML文件,其中数字用逗号分隔 <foo> <bar val="1,23"/> <bar val="4,56"/> <bar val="7,89"/> </foo> 我想对XSLT中的/foo/bar/@val值进行求和,但格式有点僵硬。有人知道正确的语法是什么吗?我猜,在“val”属性中指定的值是一个带有逗号而不是小数点的数字 有几种可能的解决方案: <foo> <bar val="

我有一个XML文件,其中数字用逗号分隔

<foo>
  <bar val="1,23"/>
  <bar val="4,56"/>
  <bar val="7,89"/>
</foo>


我想对XSLT中的
/foo/bar/@val
值进行求和,但格式有点僵硬。有人知道正确的语法是什么吗?

我猜,在
“val”
属性中指定的值是一个带有逗号而不是小数点的数字

有几种可能的解决方案

<foo>
    <bar val="1,23"/>
    <bar val="4,56"/>
    <bar val="7,89"/>
</foo>
13.68
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 exclude-result-prefixes="f xs"
 >
 <xsl:output method="text"/>
<!--                                           -->
 <xsl:template match="foo">
  <xsl:sequence select=
   "sum(bar/@val/number(translate(., ',', '.')))" 
   />
 </xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:f="http://fxsl.sf.net/"
 xmlns:my="my:fun"
 exclude-result-prefixes="my f xs"
 >
   <xsl:import href="../f/func-transform-and-sum.xsl"/>
<!--                                           -->
 <xsl:output method="text"/>
<!--                                           -->
 <xsl:template match="foo">
  <xsl:sequence select=
   "sum(
        f:transform-and-sum(my:makeNum(), bar/@val )
        )" 
   />
 </xsl:template>
<!--                                           -->
 <xsl:function name="my:makeNum" as="xs:double">
   <xsl:param name="psNum" as="xs:string"/>
<!--                                           -->
   <xsl:sequence select="number(translate($psNum, ',', '.'))"/>
 </xsl:function>
<!--                                           -->
 <xsl:function name="my:makeNum" as="element()">
   <my:makeNum/>
 </xsl:function>
<!--                                           -->
 <xsl:template match="my:makeNum" as="xs:double" mode="f:FXSL">
   <xsl:param name="arg1" as="xs:string"/>
<!--                                           -->
   <xsl:sequence select="my:makeNum($arg1)"/>
 </xsl:template>
</xsl:stylesheet>
I.XSLT 1.0

这一转变:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common"
 >
  <xsl:output method="text"/>
<!--                                           -->  
    <xsl:template match="foo">
      <xsl:variable name="vrtfBars">
        <xsl:for-each select="bar">
          <bar val="{translate(@val, ',', '.')}"/>
        </xsl:for-each>
      </xsl:variable>
<!--                                           -->
      <xsl:value-of select=
       "sum(ext:node-set($vrtfBars)/*/@val)"/>
    </xsl:template>
</xsl:stylesheet>
II。XSLT 2.0

此转换

<foo>
    <bar val="1,23"/>
    <bar val="4,56"/>
    <bar val="7,89"/>
</foo>
13.68
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 exclude-result-prefixes="f xs"
 >
 <xsl:output method="text"/>
<!--                                           -->
 <xsl:template match="foo">
  <xsl:sequence select=
   "sum(bar/@val/number(translate(., ',', '.')))" 
   />
 </xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:f="http://fxsl.sf.net/"
 xmlns:my="my:fun"
 exclude-result-prefixes="my f xs"
 >
   <xsl:import href="../f/func-transform-and-sum.xsl"/>
<!--                                           -->
 <xsl:output method="text"/>
<!--                                           -->
 <xsl:template match="foo">
  <xsl:sequence select=
   "sum(
        f:transform-and-sum(my:makeNum(), bar/@val )
        )" 
   />
 </xsl:template>
<!--                                           -->
 <xsl:function name="my:makeNum" as="xs:double">
   <xsl:param name="psNum" as="xs:string"/>
<!--                                           -->
   <xsl:sequence select="number(translate($psNum, ',', '.'))"/>
 </xsl:function>
<!--                                           -->
 <xsl:function name="my:makeNum" as="element()">
   <my:makeNum/>
 </xsl:function>
<!--                                           -->
 <xsl:template match="my:makeNum" as="xs:double" mode="f:FXSL">
   <xsl:param name="arg1" as="xs:string"/>
<!--                                           -->
   <xsl:sequence select="my:makeNum($arg1)"/>
 </xsl:template>
</xsl:stylesheet>

应用于同一XML文档时,会产生相同的正确结果

<foo>
    <bar val="1,23"/>
    <bar val="4,56"/>
    <bar val="7,89"/>
</foo>
13.68
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 exclude-result-prefixes="f xs"
 >
 <xsl:output method="text"/>
<!--                                           -->
 <xsl:template match="foo">
  <xsl:sequence select=
   "sum(bar/@val/number(translate(., ',', '.')))" 
   />
 </xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:f="http://fxsl.sf.net/"
 xmlns:my="my:fun"
 exclude-result-prefixes="my f xs"
 >
   <xsl:import href="../f/func-transform-and-sum.xsl"/>
<!--                                           -->
 <xsl:output method="text"/>
<!--                                           -->
 <xsl:template match="foo">
  <xsl:sequence select=
   "sum(
        f:transform-and-sum(my:makeNum(), bar/@val )
        )" 
   />
 </xsl:template>
<!--                                           -->
 <xsl:function name="my:makeNum" as="xs:double">
   <xsl:param name="psNum" as="xs:string"/>
<!--                                           -->
   <xsl:sequence select="number(translate($psNum, ',', '.'))"/>
 </xsl:function>
<!--                                           -->
 <xsl:function name="my:makeNum" as="element()">
   <my:makeNum/>
 </xsl:function>
<!--                                           -->
 <xsl:template match="my:makeNum" as="xs:double" mode="f:FXSL">
   <xsl:param name="arg1" as="xs:string"/>
<!--                                           -->
   <xsl:sequence select="my:makeNum($arg1)"/>
 </xsl:template>
</xsl:stylesheet>
13.68

III.

此转换

<foo>
    <bar val="1,23"/>
    <bar val="4,56"/>
    <bar val="7,89"/>
</foo>
13.68
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 exclude-result-prefixes="f xs"
 >
 <xsl:output method="text"/>
<!--                                           -->
 <xsl:template match="foo">
  <xsl:sequence select=
   "sum(bar/@val/number(translate(., ',', '.')))" 
   />
 </xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:f="http://fxsl.sf.net/"
 xmlns:my="my:fun"
 exclude-result-prefixes="my f xs"
 >
   <xsl:import href="../f/func-transform-and-sum.xsl"/>
<!--                                           -->
 <xsl:output method="text"/>
<!--                                           -->
 <xsl:template match="foo">
  <xsl:sequence select=
   "sum(
        f:transform-and-sum(my:makeNum(), bar/@val )
        )" 
   />
 </xsl:template>
<!--                                           -->
 <xsl:function name="my:makeNum" as="xs:double">
   <xsl:param name="psNum" as="xs:string"/>
<!--                                           -->
   <xsl:sequence select="number(translate($psNum, ',', '.'))"/>
 </xsl:function>
<!--                                           -->
 <xsl:function name="my:makeNum" as="element()">
   <my:makeNum/>
 </xsl:function>
<!--                                           -->
 <xsl:template match="my:makeNum" as="xs:double" mode="f:FXSL">
   <xsl:param name="arg1" as="xs:string"/>
<!--                                           -->
   <xsl:sequence select="my:makeNum($arg1)"/>
 </xsl:template>
</xsl:stylesheet>

应用于同一XML文档时会产生相同的正确结果

<foo>
    <bar val="1,23"/>
    <bar val="4,56"/>
    <bar val="7,89"/>
</foo>
13.68
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 exclude-result-prefixes="f xs"
 >
 <xsl:output method="text"/>
<!--                                           -->
 <xsl:template match="foo">
  <xsl:sequence select=
   "sum(bar/@val/number(translate(., ',', '.')))" 
   />
 </xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:f="http://fxsl.sf.net/"
 xmlns:my="my:fun"
 exclude-result-prefixes="my f xs"
 >
   <xsl:import href="../f/func-transform-and-sum.xsl"/>
<!--                                           -->
 <xsl:output method="text"/>
<!--                                           -->
 <xsl:template match="foo">
  <xsl:sequence select=
   "sum(
        f:transform-and-sum(my:makeNum(), bar/@val )
        )" 
   />
 </xsl:template>
<!--                                           -->
 <xsl:function name="my:makeNum" as="xs:double">
   <xsl:param name="psNum" as="xs:string"/>
<!--                                           -->
   <xsl:sequence select="number(translate($psNum, ',', '.'))"/>
 </xsl:function>
<!--                                           -->
 <xsl:function name="my:makeNum" as="element()">
   <my:makeNum/>
 </xsl:function>
<!--                                           -->
 <xsl:template match="my:makeNum" as="xs:double" mode="f:FXSL">
   <xsl:param name="arg1" as="xs:string"/>
<!--                                           -->
   <xsl:sequence select="my:makeNum($arg1)"/>
 </xsl:template>
</xsl:stylesheet>
13.68


最后一种解决方案更灵活,在求和之前需要对值进行更复杂的转换时,可以成功地使用。

我觉得我说了很多,但值得重复:XML的全部要点是它以易于解析的形式提供数据。包含无法解析为XML的数据的XML毫无意义;如果可能,您应该修复XML或使用不同的格式。

假设与Dimitre相同,您的意思是逗号用作十进制分隔符,而不是用作整数列表的分隔符

不带EXSLT节点集扩展的纯XSLT 1.0:

<xsl:template match="foo">
  <xsl:call-template name="sum">
    <xsl:with-param name="node" select="bar[1]"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="sum">
  <xsl:param name="node"/>
  <xsl:param name="sum" select="0"/>
  <xsl:choose>
    <xsl:when test="$node">
      <xsl:call-template name="sum">
        <xsl:with-param name="node" select="$node/following-sibling::bar[1]"/>
        <xsl:with-param name="sum" select="$sum + translate($node/@val, ',', '.')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$sum"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


非常感谢您给出了非常精确的答案:-),但世界上很多国家都使用,作为十进制分隔符。为什么呢。成为唯一被允许的人?罗伯特,我完全同意你的看法。然而,在我的例子中,XML文件是由我无法控制的第三方应用程序生成的。