xsl-list
[Top] [All Lists]

Re: [xsl] Selecting the first node set

2006-08-05 05:35:24
At 2006-08-05 12:21 +0100, Fraser Goffin wrote:
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[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:

  (//Value)[1]

Hugh explained why your prior attempts don't work ... my wording for this is as follows: //Value[1] won't work because the predicate is bound at the step level so "//" looks through the entire document and "Value[1]" which is an abbreviation for "child::Value[1]" 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.

I hope this helps, Fraser,

. . . . . . . . . . . . . . Ken

--
UBL/XML/XSLT/XSL-FO training:         Vårø, Denmark 06-09-25/10-06
World-wide corporate, govt. & user group UBL, XSL, & XML training.
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--