Copied to clipboard

Flag this post as spam?

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


  • Sean Holmesby 61 posts 82 karma points
    May 09, 2011 @ 02:26
    Sean Holmesby
    0

    Getting the nth child node

    Hi,

    I'm having trouble getting a certain child node of an item. Basically, I have a random number generator giving me a number in between 1 and the number of children. I want to then use that number to get that particular child...

    i.e random number gives me '4', I want the fourth child node.

    My random index is fine, as I have output it to the page....but I have had weird results with the item it gets, where sometimes I don't get the node, so can someone tell me if I'm doing the right thing?

    <xsl:variable name="randomNode" select="$currentPage/*[@isDoc and position() = $randomIndex] />

    How does 'position()' work? I've only ever used it in a loop before, which, at the moment, is my only other solution. (I loop through all items, and use the node only if it's position is the random index. This seems unecessary though.)

    Any help would be appreciated.

    Cheers,

    Sean

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    May 09, 2011 @ 06:59
    Chriztian Steinmeier
    1

    Hi Sean,

    You're doing the right thing, but slightly wrong :-)

    You're actually getting the correct index, but from the wrong set of nodes (i.e. all children of $currentPage, including properties).

    This will work:

    <xsl:variable name="randomNode" select="$currentPage/*[@isDoc][position() = $randomIndex]" />

    Here, the set you're selecting from only contains "Document" nodes. It's a subtle but important difference.

    Hope that explains it.

    /Chriztian

  • Sean Holmesby 61 posts 82 karma points
    May 10, 2011 @ 01:22
    Sean Holmesby
    0

    Ahhhhh.... that makes sense.... Thanks for your help Chriztian. I'll give that a go when I get home tonight.

    Cheers,

    Sean

Please Sign in or register to post replies

Write your reply to:

Draft