Re: [xsl] "not a node item" inside distinct-values

Subject: Re: [xsl] "not a node item" inside distinct-values
From: "andrew welch" <andrew.j.welch@xxxxxxxxx>
Date: Thu, 8 Jun 2006 15:17:09 +0100
On 6/8/06, Rick Roen <Rick@xxxxxxxxxxxxxxxxxx> wrote:
XSLT 2.0
XML Spy

I have the following snippet of xml:

        <item racknum="1" traynum="7" pocketnum="11" itemnum="176"
qty_ship="12">SQUASH BENNINGS GREEN TINT</item>
        <item racknum="1" traynum="7" pocketnum="12" itemnum="177"
qty_ship="42">SQUASH BLACK BEAUTY</item>
        <item racknum="1" traynum="7" pocketnum="13" itemnum="178"
qty_ship="18">SQUASH EARLY PROLIFIC STRAIGHT NECK</item>
        <item racknum="2" traynum="8" pocketnum="1" itemnum="179"
qty_ship="12">SQUASH EARLY SUMMER CROOKNECK</item>
        <item racknum="2" traynum="8" pocketnum="2" itemnum="180"
qty_ship="6">SWISS CHARD FORDHOOK GIANT</item>
        <item racknum="2" traynum="8" pocketnum="3" itemnum="183"
qty_ship="24">TOMATO CELEBRITY HYBRID</item>
        <item racknum="2" traynum="8" pocketnum="4" itemnum="204"
qty_ship="24">TOMATO FLAVOR KING HYBRID</item>
        <item racknum="2" traynum="8" pocketnum="5" itemnum="186"
qty_ship="30">WATERMELON CRIMSON SWEET</item>
        <item racknum="2" traynum="8" pocketnum="6" itemnum="205"
qty_ship="12">WATERMELON SUGAR BABY</item>


I want to get the largest @pocketnum for each @racknum, i.e. racknum = 1 largest pocketnum = 13, racknum = 2 largest pocketnum = 6

My XSLT is like this:

<xsl:for-each select="distinct-values(//item/@racknum)">
        <xsl:variable name="rack-num" select="."/>
        <xsl:variable name="pocket-num" select="//item[./@racknum =
$rack-num and position() = last()]"/>
        <p>Rack: <xsl:value-of select="$rack-num"/> max pocket:
<xsl:value-of select="$pocket-num"/></p>
</xsl:for-each>

I get an error "Error in XPath 2.0 expression Not a node item"

Oustide of the <xsl:for-each... I can use:

<xsl:value-of select="//item[./@racknum = 2 and position() =
last()]/@pocketnum"/>

and get the results I expect, i.e. 6.

This must have something to do with the context within the "for-each", but I
can't figure out how to get the information.

It does, and this I think will become a FAQ for XSLT 2.0


This context within the for-each is an item in a sequence, not a node,
so all the usual axis steps aren't available.  Apparently they did
consider having a context node and a context item - where any node
traversal would be against the node, otherwise the context would be
the item, but deemed that too confusing.  What this means is that you
need to save the context node in a variable before the context becomes
an item (within the distinct) and use that variable as the base for
node-based work:

<xsl:variable name="contextNode" select="."/>
<xsl:for-each select="distinct-values(...)">
 <xsl:value-of select="$contextNode/...........

cheers
andrew

Current Thread