Subject: Re: [xsl] Selecting the first node set|
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sat, 05 Aug 2006 08:34:51 -0400
Given the following simplified XML instance, can anyone suggest an expression that will return ONLY the 'Value' node containing <enum> children with values A, B and C. I can't rely on the name of the parent node of <Value> hence I have deliberately named these as UnknownNameX or any other intervening structure.
<Trade> <UnknownName1> <Value> <enum>A</enum> <enum>B</enum> <enum>C</enum> </Value> </UnknownName1> <UnknownName2> <Value>/Trade//Value[ancestor::Trade]</Value> </UnknownName2> <Treatment> <UnknownName3> <Value> <enum>D</enum> <enum>E</enum> <enum>F</enum> </Value> </UnknownName3> </Treatment> </Trade>
/Trade//Value - returns all 3 <Value> nodes /Trade//Value - also returns all 3 <Value> nodes (why is that ?) /Trade//Value - 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:
Hugh explained why your prior attempts don't work ... my wording for this is as follows: //Value won't work because the predicate is bound at the step level so "//" looks through the entire document and "Value" which is an abbreviation for "child::Value" returns the first child named "Value", so the combination returns "the first child named Value at every level of the document".
This is a trick question I have had in my hands-on XSLT training class since 1999 when it was still combined with DSSSL training. I've also see it at conferences where vendors post it on a white board as a "challenge" to stylesheet writers to determine who knows their XPath and who doesn't.
-- UBL/XML/XSLT/XSL-FO training: Verx, Denmark 06-09-25/10-06 World-wide corporate, govt. & user group UBL, XSL, & XML training. G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Cancer Awareness Aug'05 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal