XML Tutorial/XSLT stylesheet/sum — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:27, 26 мая 2010

round the result of sum()

File: Data.xml
<math>
 <down>
  <operand>12.12</operand>
  <operand>23.22</operand>
 </down>
 <up>
  <operand>12.15</operand>
  <operand>23.73</operand>
 </up>
</math>
File: Transform.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:template match="math">
    <xsl:apply-templates select="up|down" />
  </xsl:template>
  <xsl:template match="up|down">
    <xsl:value-of select="round(sum(operand))" />
    <xsl:text> </xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:
35 36


sum only odd ones

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <number>1</number>
    <number>3</number>
    <number>4</number>
    <number>17</number>
    <number>8</number>
    <number>11</number>
</data>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <xsl:for-each select="//number[text() mod 2 = 1]">
        <xsl:value-of select="."/>
        <xsl:choose>
    <xsl:when test="position()=last()">
      <xsl:text> = </xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text> + </xsl:text>
    </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
      <xsl:value-of select="sum(//number[text() mod 2 = 1])"/>
    </xsl:template>
</xsl:stylesheet>  
Output:
<?xml version="1.0" encoding="UTF-8"?>1 + 3 + 17 + 11 = 32


sum() sums all numbers in selected nodes.

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <number>1</number>
    <number>3</number>
    <number>4</number>
    <number>17</number>
    <number>8</number>
    <number>11</number>
</data>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <xsl:for-each select="//number">
        <xsl:value-of select="."/>
        <xsl:choose>
          <xsl:when test="position()=last()">
            <xsl:text> = </xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text> + </xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
      <xsl:value-of select="sum(//number)"/>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>1 + 3 + 4 + 17 + 8 + 11 = 44


sum()- Takes a node-set as its argument and returns the sum of the value of each individual node after converting the values to a numeric type if possible.

File: Data.xml
<?xml version="1.0"?>
<numbers>
  <x>4</x>
  <y>3.2</y>
  <z>11</z>
</numbers>

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" />
  <xsl:template match="numbers">
    4 + 3.2 + 11 =
    <xsl:value-of select="sum(*)" />
  </xsl:template>
</xsl:stylesheet>
Output:

    4 + 3.2 + 11 =
    18.2


Sum value

File: Data.xml
<math>
 <operand>12</operand>
 <operand>23</operand>
 <operand>45</operand>
 <operand>56</operand>
 <operand>75</operand>
</math>

File: Transform.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:template match="math">
    <xsl:value-of select="sum(operand)" />
  </xsl:template>
</xsl:stylesheet>
Output:
211