VextoR
VextoR

Reputation: 5165

summarize values if condition has met

for example, I have an XML:

<?xml version="1.0"?>
<A>
    <B>
        <C>true</C>
        <D>5</D>
    </B>
    <B>
        <C>false</C>
        <D>6</D>
    </B>
    <B>
        <C>true</C>
        <D>10</D>
    </B>    
</A>

if C=true I need summarize values from D, in other case just return original D value

When I use for-each or grouping I can't find a way how to summarize value if condition was met

code which is not useful at all:

<xsl:template match="/A">
    <xsl:for-each select="B">
        <xsl:value-of select="D"/>
        <xsl:text>&#10;</xsl:text> <!-- br -->
    </xsl:for-each>
</xsl:template>

I want it to return:

15
6

Upvotes: 0

Views: 67

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167581

If you know there is just the true value for which you want to sum up the D values then you don't need to group, you can just compute the sum and output the other elements e.g.

<xsl:template match="/A">
  <xsl:value-of select="sum(B[C = 'true']/D), B[not(C = 'true')]/D" separator="&#10;"/>
</xsl:template>

Upvotes: 1

PhillyNJ
PhillyNJ

Reputation: 3902

I think you can try this:

<xsl:value-of select="sum(//D[preceding-sibling::C ='true'])"/>

Upvotes: 1

Related Questions