Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sample XML:</p> <pre><code>&lt;root&gt; &lt;header/&gt; &lt;item&gt;1&lt;/item&gt; &lt;item&gt;2&lt;/item&gt; &lt;item&gt;3&lt;/item&gt; &lt;header/&gt; &lt;item&gt;5&lt;/item&gt; &lt;item&gt;6&lt;/item&gt; &lt;item&gt;7&lt;/item&gt; &lt;/root&gt; </code></pre> <p>XSLT using grouping:</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:key name="k" match="item" use="count(preceding-sibling::header)"/&gt; &lt;xsl:template match="/"&gt; &lt;root2&gt; &lt;xsl:apply-templates select="root/item[generate-id(.) = generate-id(key('k', count(preceding-sibling::header)))]" mode="a"/&gt; &lt;/root2&gt; &lt;/xsl:template&gt; &lt;xsl:template match="item" mode="a"&gt; &lt;header2&gt; &lt;xsl:apply-templates select="key('k', count(preceding-sibling::header))"/&gt; &lt;/header2&gt; &lt;/xsl:template&gt; &lt;xsl:template match="@* | node()"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@* | node()"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Or simple particular XSLT:</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:template match="/"&gt; &lt;root2&gt; &lt;header2&gt; &lt;xsl:apply-templates select="root/item[count(preceding-sibling::header) = 1]"/&gt; &lt;/header2&gt; &lt;header2&gt; &lt;xsl:apply-templates select="root/item[count(preceding-sibling::header) = 2]"/&gt; &lt;/header2&gt; &lt;/root2&gt; &lt;/xsl:template&gt; &lt;xsl:template match="@* | node()"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@* | node()"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Both produce the same output:</p> <pre><code>&lt;root2&gt; &lt;header2&gt; &lt;item&gt;1&lt;/item&gt; &lt;item&gt;2&lt;/item&gt; &lt;item&gt;3&lt;/item&gt; &lt;/header2&gt; &lt;header2&gt; &lt;item&gt;5&lt;/item&gt; &lt;item&gt;6&lt;/item&gt; &lt;item&gt;7&lt;/item&gt; &lt;/header2&gt; &lt;/root2&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