Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xml - Summing numbers with comma as decimal separator in XSLT?

I have an XML file where the number are comma-separated

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

I would like to make a sum over /foo/bar/@val values in XSLT, but I am a bit stuck the formatting. Does anyone knows what would be the proper syntax?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am guessing, that the value specified in a "val" attribute is a number that has comma instead of a decimal point.

Several solutions are possible:

I. XSLT 1.0

This transformation:

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

when applied on the originally-provided XML document:

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

produces the wanted result:

13.68

II. XSLT 2.0

This transformation:

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

when applied on the same XML document, produces the same correct result:

13.68

III. FXSL 2.x

This transformation:

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

when applied on the same XML document produces the same correct result:

13.68

The last solution is more flexible and can be used successfully when a more complex transformation of the values is needed before summing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...