0

I'm struggling with XSLT 1.0 and I hope someone can help me. This is the first time I use it. See XML below. I need to sum the Deposits for each Type. I've created a table per Type, so in the rows i show the product and in the last column I show the deposits. In the last row I want to sum the deposits for that Type. What will be the good approach?

<Letter>
    <Information>
        <Product>
            <Type>Type1</Type>
        </Product>
        <Transactions>
            <Deposits>150</Deposits>
        </Transactions>
    </Information>
</Letter>

<Letter>
    <Information>
        <Product>
            <Type>Type1</Type>
        </Product>
        <Transactions>
            <Deposits>120</Deposits>
        </Transactions>
    </Information>
</Letter>


<Letter>
    <Information>
        <Product>
            <Type>Type2</Type>
        </Product>
        <Transactions>
            <Deposits>120</Deposits>
        </Transactions>
    </Information>
</Letter>
  • https://stackoverflow.com/a/2768868/1030675 – choroba Oct 01 '18 at 09:15
  • @choroba thanks for your reply! Can you describe it some more to use it for this case? – Mike Kemperman Oct 01 '18 at 09:18
  • The linked question is slightly more complicated for your needs as it involves grouping on multiple elements, whereas you only want to group on the single `Type` element. For a fuller explanation on how Muenchian Grouping works, read up on it at http://www.jenitennison.com/xslt/grouping/muenchian.html. If you still can't work out how to implement it, post whatever you have tried and I am sure we will be able to get it to work for you. Thanks! – Tim C Oct 01 '18 at 09:28
  • To get you started, you would define your key as `` – Tim C Oct 01 '18 at 09:29

1 Answers1

0

You can use MUENCHIAN METHOD for grouping in XSLT 1.0

<xsl:key name="deposit" match="Information" use="Product/Type"/>
<xsl:template match="Letter">
    <xsl:for-each select="Information[generate-id() = generate-id(key('deposit', Product/Type)[1])]" >
        <xsl:text> Deposite for </xsl:text>
        <xsl:value-of select="Product/Type"/>
        <xsl:text> = </xsl:text>
        <xsl:value-of select="sum(key('deposit', Product/Type)/Transactions/Deposits)"/>
    </xsl:for-each> 
</xsl:template>

Output

Deposite for Type1 = 270
Deposite for Type2 = 120

See transformation at https://xsltfiddle.liberty-development.net/94hvTzQ

Rupesh_Kr
  • 3,395
  • 2
  • 17
  • 32