Re: [xsl] Selecting the first node set

Subject: Re: [xsl] Selecting the first node set
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 14 Aug 2006 12:01:13 -0400
At 08:34 AM 8/5/2006 (ages ago), Ken wrote:
/Trade//Value        - returns all 3 <Value> nodes
/Trade//Value[1]    - also returns all 3 <Value> nodes (why is that ?)
/Trade//Value[2]    - returns nothing (why is that ?)

A number of people have responded already, but from what I can tell, the two that have supplied suggested replacements haven't acknowledged that the <Value> elements are at different depths of the tree. I note that the third <Value> is at level 4 while the other two are at level 3.

So, I suggest that the following will get you the first <Value> in the document regardless of the depth of the element:



I've started teaching /descendant::Value[1] as a more intuitive and transparent solution to this little (but consequential) conundrum.

It's also reassuring to newcomers who want a simple thing to be simple, and who get scared when they learn that parentheses have such magic power without fully understanding why. One has to be willing to think with the literal-mindedness of a machine to tell the difference between /descendant-or-self::node()/child::Value[1] and (/descendant-or-self::node()/child::Value)[1], and that can take a bit of practice.


Current Thread