I need to merge certain xml nodes based on an attribute value, change that attribute value on the merged node and sum another attribute.
I am able to change the value of the attributes, but I couldn't figure out how to sum(@count) and assign it to @count on the resulting xml
Source xml
<xml>
<books category="X" count="2">
<book name="bookx1"/>
<book name="bookx2"/>
</books>
<books category="Y" count="3">
<book name="booky1"/>
<book name="booky2"/>
<book name="booky3"/>
</books>
<books category="Z" count="2">
<book name="bookz1"/>
<book name="bookz2"/>
</books></xml>
After xslt transform it needs to be like this
<xml>
<books category="A" count="5">
<book name="bookx1"/>
<book name="bookx2"/>
<book name="booky1"/>
<book name="booky2"/>
<book name="booky3"/>
</books>
<books category="Z" count="2">
<book name="bookz1"/>
<book name="bookz2"/>
</books></xml>
This is my partial xslt
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="@category">
<xsl:attribute name="category">
<xsl:choose>
<xsl:when test=".='X'">
<xsl:text>A</xsl:text>
</xsl:when>
<xsl:when test=".='Y'">
<xsl:text>A</xsl:text>
</xsl:when>
<xsl:when test=".='Z'">
<xsl:text>B</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
<xsl:template match="books[@category='X']"/>
<xsl:template match="books[@category='Y']"/></xsl:transform>