Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>This transformation</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:key name="kBooksByCat" match="books" use="@category = 'Z'"/&gt; &lt;xsl:template match="node()|@*" name="identity"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="node()|@*"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/*"&gt; &lt;xml&gt; &lt;xsl:variable name="vNonZ" select="key('kBooksByCat', 'false')"/&gt; &lt;xsl:variable name="vCatZ" select="key('kBooksByCat', 'true')"/&gt; &lt;xsl:if test="$vNonZ"&gt; &lt;books category="A" count="{sum($vNonZ/@count)}"&gt; &lt;xsl:apply-templates select="$vNonZ/node()"/&gt; &lt;/books&gt; &lt;/xsl:if&gt; &lt;xsl:if test="$vCatZ"&gt; &lt;books category="B" count="{sum($vCatZ/@count)}"&gt; &lt;xsl:apply-templates select="$vCatZ/node()"/&gt; &lt;/books&gt; &lt;/xsl:if&gt; &lt;/xml&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when applied on the provided XML document</strong>:</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><strong>produces the wanted, correct result</strong>:</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="B" count="2"&gt; &lt;book name="bookz1"/&gt; &lt;book name="bookz2"/&gt; &lt;/books&gt; &lt;/xml&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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