Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • David W. 159 posts 284 karma points c-trib
    Nov 22, 2010 @ 17:09
    David W.
    0

    If-clause before iteration

    Hello,

    I'm writing a xslt that simply list childnodes like so:

    <ul>
    <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1']">
      <li>
        <href="{umbraco.library:NiceUrl(@id)}">
          <xsl:value-of select="@nodeName"/>
        </a>
      </li>
    </xsl:for-each>
    </ul>

    However, I need to wrap this in a if-clause so that the <ul>-tags does not get rendered if there is no child nodes. Should be simple? But how?

    (I use the new schema...)


  • Rich Green 2246 posts 4008 karma points
    Nov 22, 2010 @ 17:19
    Rich Green
    1

    Not tested but should work ok

    <xsl:if test="count($currentPage/* [@isDoc and string(umbracoNaviHide) != '1']) &gt; 0">
    <ul>
    <xsl:for-each select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1']">
      <li>
        <href="{umbraco.library:NiceUrl(@id)}">
          <xsl:value-of select="@nodeName"/>
        </a>
      </li>
    </xsl:for-each>
    </ul>
    </xsl:if>

    Rich

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 22, 2010 @ 20:50
    Chriztian Steinmeier
    0

    Hi Sledger (+ Rich) - just a quick FYI about how this works in XSLT:

    Rich's solution will work just fine, though there's actually no need to count at all, because every selection will return a set of nodes that match the pattern; if no nodes are returned, the test will return false and the stuff inside won't be executed, so:

    <xsl:if test="$currentPage/*[@isDoc][not(umbracoNaviHide = 1)]">
        <ul>
            <xsl:for-each select="$currentPage/*[@isDoc][not(umbracoNaviHide = 1)]">
                <li>
                    <a href="{umbraco.library:NiceUrl(@id)}">
                        <xsl:value-of select="@nodeName"/>
                    </a>
                </li>
            </xsl:for-each>
        </ul>
    </xsl:if>
    

    /Chriztian

  • David W. 159 posts 284 karma points c-trib
    Nov 23, 2010 @ 09:03
    David W.
    0

    Thanks to both of you. I'll go with ChriZtians solution since i suppose it performs better.

    Thanks.

  • Rich Green 2246 posts 4008 karma points
    Nov 23, 2010 @ 09:04
    Rich Green
    0

    Chriztian's xslt solutions are always the best ones :)

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 23, 2010 @ 10:02
    Chriztian Steinmeier
    0

    Thanks Rich - I High-Fived yours in a feeble attempt to even out the evil takeover :-)

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft