Copied to clipboard

Flag this post as spam?

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


  • Josh Reid 182 posts 258 karma points
    Apr 08, 2010 @ 21:44
    Josh Reid
    0

    For-each inside variable

    Hi all

    I'm using the treemultipicker for a features list.

    If an included node is removed (deleted/unpublished) from the site, the features list breaks, so I am checking to make sure all the nodes exist before trying to access the properties in xslt and output, like so...

    <!-- GRAB THE REL ITEMS (PRE) -->
    <xsl:variable name="features" select="$currentPage/ancestor-or-self::node[@level=1]/data[@alias='features']"/> 
    <xsl:variable name="preItems" select="$features" />
    <xsl:variable name="preSplit" select="umbraco.library:Split($preItems, ',')" />

    <!-- TEST AND RECOMPILE REL ITEMS (REAL) -->
    <xsl:variable name="relItems">
    <xsl:for-each select="$preSplit/value">
        <xsl:if test="$currentPage/ancestor-or-self::node[@level=1]//node[@id=current()]"><xsl:value-of select="current()" />,</xsl:if>
    </xsl:for-each>
    </xsl:variable>

    <xsl:variable name="relSplit" select="umbraco.library:Split($relItems, ',')" />
    <xsl:if test="count($relSplit)&gt;0">

    <!-- COUNT, GROUP AND OUTPUT THE LIST -->

    </xsl:if>

    This code doesn't work, obviously i could just check node exists before final output of the items, but i want to count and group my node list etc before any output, so...

    A) Can i not use a for-each inside the variable or...

    B) What is wrong with the code above?

    C) Any better solution/s?

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Apr 08, 2010 @ 22:04
    Chriztian Steinmeier
    0

    Hi Josh,

    A: Yes you can - but there's a small caveat: You need to use the copy-of instruction to grab the items for the variable, and even then, you'll need to use an extension function to be able to XPath your way through the nodes in the variable - build your variable this way instead:

    <xsl:variable name="relItems-nodes">
        <xsl:for-each select="$preSplit/value">
            <xsl:copy-of select="$currentPage/ancestor-or-self::node[@id = current()]" />
        </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="relItems" select="msxml:nodeset($relItems-nodes)" />

    Now, the relItems variable holds a reference to the nodes that actually exist in the XML, and you can do your "normal" stuf (i.e. use for-each or apply-templates, with or without sorting):

    <xsl:for-each select="$relItems/node">
        <p>
            I was here...
        </p>
    </xsl:for-each>

    Hope it answers B & C too in a way... 

    /Chriztian

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

    Whoops - missed a thing in the copy-of selection:

    <xsl:copy-of select="$currentPage/ancestor-or-self::node[@level = 1]//node[@id = current()]" />

    /Chriztian

  • Josh Reid 182 posts 258 karma points
    Apr 09, 2010 @ 00:30
    Josh Reid
    0

    Thanks Chriztian!

    Alright just so the thread ends with a clean solution:

    <xsl:variable name="relItems-nodes">
            <xsl:for-each select="$preSplit/value">
            <xsl:copy-of select="$currentPage/ancestor-or-self::node[@level = 1]//node[@id = current()]" />
            </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="relItems" select="msxml:node-set($relItems-nodes)" />

    And remember in using these items if you want to get the original node (with position in the tree, etc) you need to GetXmlNodeById() to get the original node not the copy-of.

    JR

Please Sign in or register to post replies

Write your reply to:

Draft