Note that there are some explanatory texts on larger screens.

plurals
  1. POneed to merge xml nodes based on an attribute value
    text
    copied!<p>I need to merge certain xml nodes based on an attribute value, change that attribute value on the merged node and sum another attribute.</p> <p>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</p> <p>Source xml</p> <pre><code>&lt;xml&gt; &lt;books category="X" count="2"&gt; &lt;book name="bookx1"/&gt; &lt;book name="bookx2"/&gt; &lt;/books&gt; &lt;books category="Y" count="3"&gt; &lt;book name="booky1"/&gt; &lt;book name="booky2"/&gt; &lt;book name="booky3"/&gt; &lt;/books&gt; &lt;books category="Z" count="2"&gt; &lt;book name="bookz1"/&gt; &lt;book name="bookz2"/&gt; &lt;/books&gt;&lt;/xml&gt; </code></pre> <p>After xslt transform it needs to be like this</p> <pre><code>&lt;xml&gt; &lt;books category="A" count="5"&gt; &lt;book name="bookx1"/&gt; &lt;book name="bookx2"/&gt; &lt;book name="booky1"/&gt; &lt;book name="booky2"/&gt; &lt;book name="booky3"/&gt; &lt;/books&gt; &lt;books category="Z" count="2"&gt; &lt;book name="bookz1"/&gt; &lt;book name="bookz2"/&gt; &lt;/books&gt;&lt;/xml&gt; </code></pre> <p>This is my partial xslt</p> <pre><code>&lt;xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml"/&gt; &lt;xsl:template match="*"&gt; &lt;xsl:element name="{local-name()}"&gt; &lt;xsl:apply-templates select="@*|node()"/&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;xsl:template match="@*"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="@category"&gt; &lt;xsl:attribute name="category"&gt; &lt;xsl:choose&gt; &lt;xsl:when test=".='X'"&gt; &lt;xsl:text&gt;A&lt;/xsl:text&gt; &lt;/xsl:when&gt; &lt;xsl:when test=".='Y'"&gt; &lt;xsl:text&gt;A&lt;/xsl:text&gt; &lt;/xsl:when&gt; &lt;xsl:when test=".='Z'"&gt; &lt;xsl:text&gt;B&lt;/xsl:text&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:value-of select="."/&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:attribute&gt; &lt;/xsl:template&gt; &lt;xsl:template match="books[@category='X']"/&gt; &lt;xsl:template match="books[@category='Y']"/&gt;&lt;/xsl:transform&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload