Re: [xsl] Question about position()

Subject: Re: [xsl] Question about position()
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 14 May 2024 18:03:26 -0000
You're missing the fact that the value of position() is context-sensitive:
inside square brackets, it means the position of the item you are examining,
which is different from the value of position() outside the predicate.

Michael Kay
Saxonica

> On 14 May 2024, at 16:33, rick@xxxxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Hi All,
>
> I am creating sorted variable in order to use as a lookup later. Later, I
want to loop through the nodes and return its code attribute value one at a
time. When I use the position() function as a predicate, it returns all of the
attributes instead of the one at that position. If I put a hard-coded number
in as the predicate, it returns a single value, which is what I expect. I am
missing something fundamental here. Thank you for the help.
>
> <?xml version="1.0" encoding="UTF-8"?>
> <root>
>     <features>
>         <feature code="5521" text="Cryopreserved"/>
>         <feature code="6751" text="Assigned HCPCS code"/>
>         <feature code="5551" text="Shelf life greater than 2 years"/>
>         <list count="3"/>
>     </features>
> </root>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>     xmlns:xs="http://www.w3.org/2001/XMLSchema";
>     xmlns:math="http://www.w3.org/2005/xpath-functions/math";
>     exclude-result-prefixes="xs math"
>     version="3.0" expand-text="yes">
>
>     <xsl:output indent="yes"/>
>
>     <xsl:template match="/root">
>         <xsl:variable name="features" as="node()*">
>             <xsl:perform-sort select="features/feature">
>                 <xsl:sort select="@text"/>
>             </xsl:perform-sort>
>         </xsl:variable>
>         <xsl:for-each select="1 to count($features)">
>             <xsl:message>{position()}</xsl:message>
>             <!-- Why does this return all three... -->
>             <xsl:message>{$features[position()]/@code}</xsl:message>
>             <!-- while this one returns one? -->
>             <xsl:message>{$features[1]/@code}</xsl:message>
>         </xsl:for-each>
>     </xsl:template>
>
> </xsl:stylesheet>
>
> Thank you!
> Rick
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by
email <>)

Current Thread