Re: [xsl] lookup table keyed by position

Subject: Re: [xsl] lookup table keyed by position
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 23 Feb 2005 15:09:17 -0500

At 03:02 PM 2/23/2005, you wrote:
I tried to build a lookup table that would convert 2-digit month numbers
to their 3-letter abbreviation like this:

<xsl:variable name="months">

And given a variable in YYYY-MM-DD format, I can get the abbreviation
with this expression:

$months/abbrev[number(substring($yyyy-mm-dd, 6, 2))]

Then I tried to index the table like this:

<xsl:key name="months-by-position" match="abbrev" use="position()"/>

And look up the abbreviation with this expression:

key('months-by-position', number(substring($yyyy-mm-dd, 6, 2)), $months)

But my output indicates that for all of the '02' through '12' numbers,
the key function returns an empty sequence, and for '01' it returns a
sequence of all the abbrev elements (which are serialized with a space
between them).

It seems that the context node when position is evaluated is the months
element, not the individual abbbrev elements.  Can this be made to work
as I want?

No, it's the abbrev -- but each of them is getting assigned position() = 1.

This is because of the way position() works, which is different from what you're expecting. It is specified to return not a number representing the position of a node among its siblings, but one representing the position of a node in a given processing context (the "current node list"). Since there is none such when you declare a key, position() is pretty well useless here: each node is taken to be the first in its current node list, so they're all '1'.

You might have better luck with:

<xsl:key name="months-by-position" match="abbrev" use="count(preceding-sibling::*) + 1"/>

which actually assigns the index number you want.


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================

Current Thread