Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your XSLT is very <a href="http://www.ibm.com/developerworks/library/x-xdpshpul.html" rel="nofollow noreferrer">"pull" focused rather than "push"</a> focused and could use some re-engineering to be more <em>functional</em> (in the functional programming sense).</p> <p>But the main error is here:</p> <pre><code>&lt;!-- Infinity and beyond (=&gt; Level 3) --&gt; &lt;xsl:template name="folder"&gt; </code></pre> <p>This is declaring a <a href="http://msdn.microsoft.com/en-us/library/ms256459%28v=vs.110%29.aspx" rel="nofollow noreferrer">"named template" but it's never called</a>. Not a bad thing, as <a href="https://stackoverflow.com/questions/4478045/what-are-the-difference-between-call-template-and-apply-templates-in-xsl">name/call-templates should be avoided where possible.</a></p> <p>Change it to the below and it should work as expected when using the <code>apply-templates</code> you already have in the XSLT.</p> <pre><code>&lt;xsl:template match="folder"&gt; </code></pre> <p><strong>edit: A push style approach</strong></p> <p>This might not be perfect, but a few things: * You skip over <code>&lt;folders&gt;</code> entirely, when they <em>are the list</em>, where as the <code>&lt;folder&gt;</code>s are the list items. * You appear to duplicate the item code for levels 1, 2 and 3. * You only ever make one item in the level 3 and above, all others would be ignored?</p> <p>Notice below, that I've defined how I want <code>&lt;folders&gt;</code> to be rendered, based on level, and how <code>&lt;folder&gt;</code>s are rendered independently.</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt; &lt;xsl:output method="html" omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:template match="/"&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="folders"&gt; &lt;ul&gt; &lt;xsl:attribute name="class"&gt; &lt;xsl:text&gt;nav-l&lt;/xsl:text&gt; &lt;xsl:choose&gt; &lt;xsl:when test="@level&amp;gt;2"&gt; &lt;xsl:text&gt;s nav-hidden&lt;/xsl:text&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:value-of select="@level"/&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:attribute&gt; &lt;xsl:apply-templates/&gt; &lt;/ul&gt; &lt;/xsl:template&gt; &lt;xsl:template match="folder"&gt; &lt;li&gt; &lt;xsl:if test="@selected='Y'"&gt; &lt;xsl:attribute name="class"&gt;nav-active&lt;/xsl:attribute&gt; &lt;/xsl:if&gt; &lt;a href="{@url}" class="nav-item"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="child::*"&gt; &lt;xsl:attribute name="class"&gt;trigger-chan nav-next&lt;/xsl:attribute&gt; &lt;span&gt; &lt;span class="trigger-cntr"&gt; &lt;xsl:value-of select="@name"/&gt; &lt;/span&gt; &lt;/span&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;span&gt; &lt;xsl:value-of select="@name"/&gt; &lt;/span&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/a&gt; &lt;xsl:apply-templates select="folders"/&gt; &lt;/li&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Notice how the above template is shorter than your initial one, as repeated elements are removed. All <code>&lt;folder&gt;</code>s render the same, so only one template is needed. In fact, the <code>&lt;folder&gt;</code>s don't need to know their level, only the parent <code>&lt;folders&gt;</code> do, so that logic is moved to the correct template.</p> <p>People who are new to XSLT often overlook how <code>apply-templates</code> can be used effectively to define recursion and allow very clear separations between elements. When creating templates, try and focus on how <em>this</em> particular element should be represented, and knowing how its child elements may be required. Then, focus on a new template for the child elements and how those should be rendered and so on.</p> <p>Adding on to the above, always be on the look out for <code>../</code> in <code>@select</code>s or <code>&lt;value-of&gt;</code>s as it implies that your logic is dependant on elements that are outside the current "scope". Not always bad, but something to be aware of.</p> <p>Try and minimise <code>for-each</code> where possible and see if they can be rewritten to use <code>apply-templates</code>, and unless absolutely required for some odd reason try and never use <code>call-templates</code> as they are less <em>functional</em>.</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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