Re: [xsl] Getting specific elements from a list

Subject: Re: [xsl] Getting specific elements from a list
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Fri, 27 May 2005 07:07:40 +1000
>        <xsl:value-of select=".//EnumValueDescription[1]"/>,
>        <xsl:value-of select=".//EnumValueDescription[last()]"/>


In the source xml provided every "EnumValueDescription" element is the
only "EnumValueDescription" - child of its parent. This means that it
is both the first and the last such child.

But the XPath expression:

 //elemName[k]

selects all "elemName" elements that are the k-th child (with this
same name) of their parent (read more in the FAQs about this
pequliarity of using the "//" abbreviation).

Therefore,

   .//EnumValueDescription[1]

and

   .//EnumValueDescription[last()]

select all "EnumValueDescription" descendents of the current node.

Then,

    xsl:value-of

with a "select" attribute specifying a node-set of more than one node,

outputs the string value of the first node (in document order) of these.

This is how the value "Red" is produced in both cases.


The correct XPath expressions to use are, therefore:

   (.//EnumValueDescription)[1]

and

   (.//EnumValueDescription)[last()]



Of course, in this particular case it is best *not* to use the "//"
abbreviation. What would suffice is just:

   tblEnumValues/EnumValueDescription[1]

and

   tblEnumValues/EnumValueDescription[last()]


Cheers,
Dimitre Novatchev

On 5/27/05, Simon, Jb <jb.simon@xxxxxxxx> wrote:
>
> Hi, I have the following XML snippit :
>
> <tblEnumeratedTypes>
>        <EnumeratedType>ColorType</EnumeratedType>
>        <tblEnumValues>
>                <EnumeratedType>ColorType</EnumeratedType>
>                <EnumValueDescription>Red</EnumValueDescription>
>        </tblEnumValues>
>        <tblEnumValues>
>                <EnumeratedType>ColorType</EnumeratedType>
>                <EnumValueDescription>White</EnumValueDescription>
>        </tblEnumValues>
>        <tblEnumValues>
>                <EnumeratedType>ColorType</EnumeratedType>
>                <EnumValueDescription>Blue</EnumValueDescription>
>        </tblEnumValues>
> </tblEnumeratedTypes>
>
> What I need as output is
>
> TEST ( int, ColorType, Red, Blue )
>
> The problem I'm having is getting the Red and Blue, basically, the
> First and last EnumeratedType element of EnumeratedTypes.
>
> I tried various combinations, although it seems this should have
> worked...
>
>        <xsl:template match="tblEnumeratedTypes_WC" mode="range-macro">
>
>
> test (
>        int,
>        <xsl:value-of select="EnumeratedType"/>,
>        <xsl:value-of select=".//EnumValueDescription[1]"/>,
>        <xsl:value-of select=".//EnumValueDescription[last()]"/>
> ) ;
>
>        </xsl:template>
>
> When I execute that I get (line breaks are not a problem)
>
> TEST (
>        int,
>        ColorType,
>        Red,
>        Red
>        ) ;
>
> What is the problem is that I can never get the last Value to be Blue.
>
> Oh, XSL Procesor : Microsoft (R) XSLT Processor Version 4.0
> Also tried it using Mozilla, same result
>
> Any Ideas ?
>
> TIA
> Joe Simon

Current Thread