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.