Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't need to do anything fancy like recursion. And good lord, don't even contemplate using CDATA. </p> <p>You just have to think like XSLT and ask: "What input element do I want to transform into my output element?"</p> <p>Assuming that each <code>ul</code> is supposed to contain N <code>item</code>s, you want to transform every Nth input <code>item</code>, starting with the first, into a <code>ul</code>:</p> <pre><code>&lt;xsl:variable name="n" select="number(4)"/&gt; &lt;xsl:template match="/"&gt; &lt;output&gt; &lt;xsl:apply-templates select="/root/item[position() mod $n = 1]"/&gt; &lt;/output&gt; &lt;/xsl:template&gt; </code></pre> <p>Each of these <code>item</code> elements becomes a <code>ul</code> that contains the item and each of its N-1 following siblings:</p> <pre><code>&lt;xsl:template match="item"&gt; &lt;ul&gt; &lt;xsl:for-each select=". | following-sibling::item[position() &amp;lt; $n]"&gt; &lt;li&gt; &lt;xsl:value-of select="."/&gt; &lt;/li&gt; &lt;/xsl:for-each&gt; &lt;/ul&gt; &lt;/xsl:template&gt; </code></pre> <p>Assuming an input document like this:</p> <pre><code>&lt;root&gt; &lt;item&gt;1&lt;/item&gt; &lt;item&gt;2&lt;/item&gt; &lt;item&gt;3&lt;/item&gt; &lt;item&gt;4&lt;/item&gt; &lt;item&gt;5&lt;/item&gt; &lt;item&gt;6&lt;/item&gt; &lt;item&gt;7&lt;/item&gt; &lt;item&gt;8&lt;/item&gt; &lt;item&gt;9&lt;/item&gt; &lt;/root&gt; </code></pre> <p>...you get this output, if <code>$n</code> is set to 4:</p> <pre><code>&lt;output&gt; &lt;ul&gt; &lt;li&gt;1&lt;/li&gt; &lt;li&gt;2&lt;/li&gt; &lt;li&gt;3&lt;/li&gt; &lt;li&gt;4&lt;/li&gt; &lt;/ul&gt; &lt;ul&gt; &lt;li&gt;5&lt;/li&gt; &lt;li&gt;6&lt;/li&gt; &lt;li&gt;7&lt;/li&gt; &lt;li&gt;8&lt;/li&gt; &lt;/ul&gt; &lt;ul&gt; &lt;li&gt;9&lt;/li&gt; &lt;/ul&gt; &lt;/output&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