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.)
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
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:
Here, the set you're selecting from only contains "Document" nodes. It's a subtle but important difference.
Hope that explains it.
/Chriztian
Ahhhhh.... that makes sense.... Thanks for your help Chriztian. I'll give that a go when I get home tonight.
Cheers,
Sean
is working on a reply...