0
votes

I am beginner in XSLT and using it to transform XML to XML through Java.

Source XML:

<Response>
    <Data>
        <Book name="A" value="1"/>
        <Book name="B" value="2"/>
        <Book name="C" value="1"/>
    </Data>
    <Result>
        <Prices>
            <Price type="A" value="100"/>
            <Price type="B" value="60"/>
            <Price type="C" value="40"/>
        </Prices>
    </Result>
</Response>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="/">
        <xsl:element name="Books">
            <xsl:variable name="BookType" select="//@type" />
            <xsl:attribute name="Total">
                <xsl:value-of select="sum(//Price[@type=$BookType]/@value)"/>
            </xsl:attribute>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Output XML:

<Books Total="200"/>

Expected Output XML:

<Books Total="260"/>

In source XML i receive no of book and its price but they are not relevant.

<Price> tag indicates the price of one book. I need to calculate the total price of all the books as below

Price of one book x no of books
For A : 100 x 1 = 100
For B : 60 x 2  = 120
For C : 40 x 1  = 040
------------------------
Total Price is  = 260

Please help.

1

1 Answers

1
votes

The easiest approach would be to build up a temporary structure in a variable containing the "price * qty" values, then use the exslt:node-set extension function to sum those values

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:exslt="http://exslt.org/common"
                exclude-result-prefixes="exslt">
  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/">
    <xsl:variable name="subTotals">
      <xsl:for-each select="/Response/Data/Book">
        <totalPrice>
          <xsl:value-of select="@value *
            /Response/Result/Prices/Price[@type = current()/@name]/@value" />
        </totalPrice>
      </xsl:for-each>
    </xsl:variable>
    <Books Total="{sum(exslt:node-set($subTotals)/totalPrice)}" />
  </xsl:template>
</xsl:stylesheet>

It is possible to do this in pure XSLT 1.0 without using the extension function, but it's rather fiddly and involves passing an accumulator parameter down a chain of templates:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/">
    <Books>
      <xsl:apply-templates select="/Response/Data/Book[1]" />
    </Books>
  </xsl:template>

  <xsl:template match="Book">
    <xsl:param name="runningTotal" select="0" />
    <xsl:variable name="thisBookTotal" select="@value *
       /Response/Result/Prices/Price[@type = current()/@name]/@value" />
    <xsl:choose>
      <xsl:when test="following-sibling::Book">
        <!-- There are more books, process the next one, passing an updated
             running total -->
        <xsl:apply-templates select="following-sibling::Book[1]">
          <xsl:with-param name="runningTotal" select="$runningTotal + $thisBookTotal" />
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <!-- There are no more books, produce the final total -->
        <xsl:attribute name="Total">
          <xsl:value-of select="$runningTotal + $thisBookTotal" />
        </xsl:attribute>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>