[xsl] Question about position()

Subject: [xsl] Question about position()
From: "rick@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 14 May 2024 15:32:55 -0000
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

Current Thread