Re: [xsl] What's wrong with my ancestor syntax?

Subject: Re: [xsl] What's wrong with my ancestor syntax?
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 27 Mar 2007 17:18:52 +0100
>> 	<xsl:for-each select="//menuItem[pageID =  
>> $pageID]/descendant-or-self::menuItem"> 

>I think it says:
>
>Select any menuItem (//menuItem)
>That has a pageID of x ([pageID = $pageID])
>And is or is a descendant of said page (/descendant-or-self::menuItem)
>
>Did I get that about right? 

No, you have to read each step of an xpath in the context of where you
ended up at the last step (er, rather like walking:-)

so you read it as
Select any menuItem (//menuItem)That has a pageID of x ([pageID = $pageID])
Then FROM THERE select that node and any descendant menuItem nodes (descendant-or-self::menuItem),
return the resulting set of nodes.

This is just the same as an xpath of
/a/b/c
which means select all child a
then from those nodes, select all child b 
then from there select all child c nodes
return the resulting node set (or sequence in xpath 2)

whenever you see an an xpath of the form //blrgh[fidget=$wibble] you
should be asking yourself whether you should be using instead
<xsl:key name="x" match="blrgh" use="fidget"/>
...
  select="key('x',$wibble)"

since an key essentially just tells the system to optimise such lookups,
but get working first, before optimising!

David

Current Thread