Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Add up elements value by matching name element

Add up elements value by matching name element

Scheduled Pinned Locked Moved XML / XSL
xmlhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RossBradley
    wrote on last edited by
    #1

    Hi - Any help or guidance would be appreciated. I am trying a new xml file with the total of one of the specific elements - below is the original xml file:

    <Report>
    <Unit>
    <Name>t1</Name>
    <Price>32</Price>
    <Other>sdasd</Other>
    </Unit>
    <Unit>
    <Name>t1</Name>
    <Price>42</Price>
    <Other>dasdasd</Other>
    </Unit>
    <Unit>
    <Name>t2</Name>
    <Price>11</Price>
    <Other>asdas</Other>
    </Unit>
    <Unit>
    <Name>t3</Name>
    <Price>21</Price>
    <Other>dasd</Other>
    </Unit>
    <Unit>
    <Name>t1</Name>
    <Price>65</Price>
    <Other>dasda</Other>
    </Unit>

    The xsl that I am trying to us is as follows: instead of giving me 1 value it gives me a list of all t1 values rather than a sumation

    <test>
    <xsl:if test="Name = 't1'">
    <xsl:variable name="value1" select ="sum(Price)"/>
    <xsl:value-of select="$value1"/>
    </xsl:if>
    </test>

    thanks

    S 1 Reply Last reply
    0
    • R RossBradley

      Hi - Any help or guidance would be appreciated. I am trying a new xml file with the total of one of the specific elements - below is the original xml file:

      <Report>
      <Unit>
      <Name>t1</Name>
      <Price>32</Price>
      <Other>sdasd</Other>
      </Unit>
      <Unit>
      <Name>t1</Name>
      <Price>42</Price>
      <Other>dasdasd</Other>
      </Unit>
      <Unit>
      <Name>t2</Name>
      <Price>11</Price>
      <Other>asdas</Other>
      </Unit>
      <Unit>
      <Name>t3</Name>
      <Price>21</Price>
      <Other>dasd</Other>
      </Unit>
      <Unit>
      <Name>t1</Name>
      <Price>65</Price>
      <Other>dasda</Other>
      </Unit>

      The xsl that I am trying to us is as follows: instead of giving me 1 value it gives me a list of all t1 values rather than a sumation

      <test>
      <xsl:if test="Name = 't1'">
      <xsl:variable name="value1" select ="sum(Price)"/>
      <xsl:value-of select="$value1"/>
      </xsl:if>
      </test>

      thanks

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      This XSL stylesheet will output a single 'test' tag containing the total you want. Note that I construct a list of all the 'Price' tags (with //Price) and filter that list with a predicate on the sibling Name tag ([../Name='t1']).

      <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      <xsl:template match="/">
      <test>
      <xsl:value-of select="sum(//Price[../Name='t1'])"/>
      </test>
      </xsl:template>

      </xsl:stylesheet>

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups