Copied to clipboard

Flag this post as spam?

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


  • Connie DeCinko 931 posts 1160 karma points
    May 04, 2011 @ 16:51
    Connie DeCinko
    0

    How to get all sibling nodes and their children

    How do I get all the sibling nodes and their children?

     

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    May 04, 2011 @ 17:58
    Dirk De Grave
    2

    Can't you traverse up one level starting from the current node, and traverse the parent node (and probably ignoring the current node, aka $currentPage) and child nodes (will probably need some recursive template to get all descendant nodes for each of the siblings)

     

    Hope this helps.

    Regards,

    /Dirk

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    May 04, 2011 @ 23:20
    Chriztian Steinmeier
    0

    Hi Connie,

    As Dirk says, you can grab all siblings of a given node by selecting the children of the parent node:

    <!-- Grab all siblings, including $currentPage -->
    <xsl:variable name="allSiblings" select="$currentPage/../*[@isDoc]" />
    
    <!-- Exclude $currentPage from the set -->
    <xsl:variable name="siblingsOnly" select="$currentPage/../*[@isDoc][not(@id = $currentPage/@id)]" />
    

     - those variables have access to the individual nodes' children (and grandchildren, ancestors etc.) - just use the standard XPath axes to get to them.

    /Chriztian

  • Connie DeCinko 931 posts 1160 karma points
    May 05, 2011 @ 00:31
    Connie DeCinko
    0

    Well, something is not working right.

    This gets every single child node of the parent including grandchildren:

     <xsl:for-each select="$currentPage/..//*">
      <p><xsl:value-of select="@nodeName"/>
     </xsl:for-each>

    This however returns nothing:

      <xsl:key name="years" match="*" use="Exslt.ExsltDatesAndTimes:year(publicationDate)"/>
      <xsl:key name="months" match="*" use="umbraco.library:FormatDateTime(@publicationDate, 'yyyy-MM')"/>

     <xsl:template match="/">
     
       <xsl:for-each select="$currentPage/..//* [(generate-id()=generate-id(key('years', Exslt.ExsltDatesAndTimes:year(publicationDate))[1]))]">
              <p><xsl:value-of select="@nodeName"/></p>
       </xsl:for-each>
     </xsl:template>

    So, there must be something different I have to do for setting the key???

     

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    May 05, 2011 @ 00:56
    Chriztian Steinmeier
    1

    Hi Connie,

    To me, it just looks like you've forgot to add current() to get the publicationDate for the current item in the for-each (in the second half of the comparison). 

    Also: If publicationDate is a standard xmlDate field (e.g.  something like: "2011-12-28T12:34:00+0200") there's no need to use extension functions to extract the data you need - example code below:

    <xsl:key name="years" match="*" use="substring(publicationDate, 1, 4)" />
    <xsl:key name="months" match="*" use="substring(publicationDate, 1, 7)" />
    
    <xsl:template match="/">
        <xsl:for-each select="$currentPage/..//*[generate-id() = generate-id(key('years', substring(current()/publicationDate, 1, 4))[1])]">
            <p><xsl:value-of select="@nodeName"/></p>
        </xsl:for-each>
    </xsl:template>

    /Chriztian

     

     

  • Connie DeCinko 931 posts 1160 karma points
    May 05, 2011 @ 22:06
    Connie DeCinko
    0

    I added current() and still get no results.  Used your exact code, and still, no results.

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 05, 2011 @ 22:08
    Jan Skovgaard
    0

    Hi Connie

    And are you using the new or the old XML schema?

    /Jan

  • Connie DeCinko 931 posts 1160 karma points
    May 05, 2011 @ 22:13
    Connie DeCinko
    0

    New schema.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    May 06, 2011 @ 01:00
    Chriztian Steinmeier
    0

    Hi Connie,

    My bad - this is very tricky; in that exact setting, you should not use current() - this works for me, but it's really just using substring() instead of the extension function:

    <!-- XML -->
    <root>
        <Website id="1199" level="1" isDoc="" nodeName="Home">
            <Textpage id="1200" level="2" nodeName="Page 1" isDoc="">
                <publicationDate>2011-12-12T00:29:12+0200</publicationDate>
            </Textpage>
            <Textpage id="1201" level="2" nodeName="Page 2" isDoc="">
                <publicationDate>2011-11-05T00:29:12+0200</publicationDate>
            </Textpage>
            <Textpage id="1202" level="2" nodeName="Page 3" isDoc="">
                <publicationDate>2011-12-08T00:29:12+0200</publicationDate>
            </Textpage>
            <Textpage id="1203" level="2" nodeName="Page 4" isDoc="">
                <publicationDate>2009-05-06T00:29:12+0200</publicationDate>
            </Textpage>
            <Textpage id="1204" level="2" nodeName="Page 5" isDoc="">
                <publicationDate>2010-05-07T00:29:12+0200</publicationDate>
            </Textpage>
            <Textpage id="1205" level="2" nodeName="Page 6" isDoc="">
                <publicationDate>2008-04-06T00:29:12+0200</publicationDate>
            </Textpage>
        </Website>
    </root>
    
    <!-- XSLT -->
    <xsl:key name="years" match="*" use="substring(publicationDate, 1, 4)" />
    <xsl:key name="months" match="*" use="substring(publicationDate, 1, 7)" />
    
    <xsl:template match="/">
        <xsl:for-each select="$currentPage/..//*[generate-id() = generate-id(key('years', substring(publicationDate, 1, 4))[1])]">
            <p><xsl:value-of select="@nodeName"/></p>
        </xsl:for-each>
    </xsl:template>

     

    If this still doesn't work, I guess it'd be necessary to have a look at your XML, if possible?

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft