tags:

views:

76

answers:

1

I have the following XML:

    <root>
 <groups>
  <group id="101">
   <number>10</number>
   <children>
    <leader id = "A" />
    <members>
     <member id="1">
     <member id="2">
     <member id="3">
    </members>
   </children>
  </group>
  <group id="102">
   <number>20</number>
   <children>
    <leader id = "A" />
    <members>
     <member id="4">
     <member id="5">
     <member id="3">
    </members>
   </children>
  </group>
  <group id="103">
   <number>30</number>
   <children>
   <leader id = "A" />
    <members>
     <member id="1">
     <member id="4">
     <member id="3">
    </members>
   </children>
  </group>
 </groups>

</root>

How can I sum the property of all the member whom id = 1 using XSL?

Thanks

+3  A: 

Use:

sum(/*/*/*/number[../children/members/member[@id=1]])

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output method="text"/>

 <xsl:template match="/">
  <xsl:value-of select=
  "sum(/*/*/*/number
              [../children/members/member[@id=1]])
  "/>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document (corrected the severely malformed provided text!):

<root>
    <groups>
        <group id="101">
            <number>10</number>
            <children>
                <leader id = "A" />
                <members>
                    <member id="1"/>
                    <member id="2"/>
                    <member id="3"/>
                </members>
            </children>
        </group>
        <group id="102">
            <number>20</number>
            <children>
                <leader id = "A" />
                <members>
                    <member id="4"/>
                    <member id="5"/>
                    <member id="3"/>
                </members>
            </children>
        </group>
        <group id="103">
            <number>30</number>
            <children>
                <leader id = "A" />
                <members>
                    <member id="1"/>
                    <member id="4"/>
                    <member id="3"/>
                </members>
            </children>
        </group>
    </groups>
</root>

produces the wanted, correct result:

40
Dimitre Novatchev
@Dimitre: +1 for rigth answer and for taking the time to correct a "severely malformed" input! Ja!
Alejandro