Re: [xsl] Fun with xsl:number and a key

Subject: Re: [xsl] Fun with xsl:number and a key
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 14 Oct 2004 18:25:24 -0400
Hi Jay,

You're asking the right questions....

At 01:55 PM 10/14/2004, you wrote:
My question is this: Why does the following bit not generate a multi-level
number?
      <xsl:for-each select="key('refentries', .)">
        <xsl:number level="multiple"/>
      </xsl:for-each>

Personally, I speculate that the cause is the nature of the key, which
matches an attribute and then uses . If I understand correctly, this key
holds the text of the attribute, which ditches the context information.

No, it returns the node, in this case a @title attribute.


Why xsl:number is doing what it's doing, I can't exactly say without digging into the spec (http://www.w3.org/TR/xslt#number) and testing. My guess, however, is that you're running into a clash between how you're using level="multiple" and the default on the 'count' attribute (okay now I'm in the spec):

'When level="multiple", it constructs a list of all ancestors of the current node in document order followed by the element itself; ***it then selects from the list those nodes that match the count pattern***; it then maps each node in the list to one plus the number of preceding siblings of that node that match the count pattern. If the from attribute is specified, then the only ancestors that are searched are those that are descendants of the nearest ancestor that matches the from pattern. Preceding siblings has the same meaning here as with the preceding-sibling axis.'

Your count pattern being "@title" (it has defaulted to the name and node type of the current node), xsl:number does not count the chapter it's inside of. And since attributes have no preceding siblings, you always get 1.

One way to go would be to say <xsl:number level="multiple" count="chapter"/>, but even better would be to set up your key differently --

<xsl:key name="refentries" match="chapter" use="@title"/>

or even

<xsl:key name="refentries" match="*[@title]" use="@title"/>

since with either of these, you'd get the actual chapter, not its @title attribute, back from the key() function, and would be counting chapters not their @title attributes.

Cheers,
Wendell


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 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