XML/XSL: Sum in foreach loop with conditional (special case)
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>
开发者_C百科 <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
Use:
sum(/*/*/*/number[../children/members/member[@id=1]])
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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
精彩评论