Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is easy. The XSLT processor does all the recursion and the looping for you, all you need to do is to specify templates for nodes you want to handle.</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" &gt; &lt;!-- &lt;items&gt; with &lt;item&gt; children becomes &lt;ul&gt; --&gt; &lt;xsl:template match="items[item]"&gt; &lt;ul&gt; &lt;xsl:apply-templates select="item" /&gt; &lt;/ul&gt; &lt;/xsl:template&gt; &lt;!-- &lt;items&gt; without &lt;item&gt; children is not handled --&gt; &lt;xsl:template match="items[not(item)]" /&gt; &lt;!-- &lt;item&gt; with @name becomes &lt;li&gt; --&gt; &lt;xsl:template match="item[@name]"&gt; &lt;li&gt; &lt;xsl:value-of select ="@name" /&gt; &lt;xsl:apply-templates select="items" /&gt; &lt;/li&gt; &lt;/xsl:template&gt; &lt;!-- &lt;item&gt; without @name becomes &lt;li&gt;, too --&gt; &lt;xsl:template match="item[not(@name)]"&gt; &lt;li&gt; &lt;xsl:value-of select ="id" /&gt; &lt;xsl:apply-templates select="items" /&gt; &lt;/li&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>The <code>&lt;xsl:apply-templates&gt;</code> always is the recursive/iterative step in XSLT. It takes any nodes that fit its <code>select</code> expression and finds templates for them.</p> <p>Your job is it to craft an appropriate <code>select</code> expression, supply a template for every node you want to handle and otherwise get out of the way. ;-) Resist the urge to cram everything into one big template or use <code>&lt;xsl:for-each&gt;</code> just because it feels convenient - it is not. Separate templates create more reusable and maintainable, less deeply nested code, and XSLT processors are optimized for template handling, so this might even be the more efficient approach.</p>
 

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