views:

529

answers:

2

Is there a more direct way in umbraco to iterate nodes based on a list of IDs than this?

$currentPage/ancestor-or-self::root/descendant::node[contains($idList, @id)]

I'm just curious. It seems akward to traverse up the tree from the currentPage node to find the root before traversing back down.

A: 

You could use the @path attribute which contains a csv list of the nodes ancestor ids. Then just grab the first one or whichever one is of interest.

Another technique could be to use @level to select a node in the ancestors at a particular depth in the tree.

Not in front of my computer at the moment will update with more detail when I am.

Tim Saunders
A: 

It would probably be both more efficient and easier to define an xsl:key on the nodes you're interested in and then retrieve those using the key() function.

<xsl:key name="node-ids" match="node" use="id"/>
<!-- put the "at" sign in front of "id",
  a blockquote is generated when I try it -->
<xsl:for-each select="$currentPage">
  <xsl:apply-templates select="key( 'node-by-id', $idList)"/>
</xsl:for-each>

See: key() function (spec). Note how the second argument may be of type node-set. If your $idList is a comma-separated string, you might want to change to node-set.