[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] xsl:number and including nodes with xsl:key


Subject: Re: [xsl] xsl:number and including nodes with xsl:key
From: Bill Keese <billk@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 29 Aug 2003 13:34:28 +0900

<xsl:number> has no "context". Calling <xsl:number> does not increment a counter.

Rather, <xsl:number> reports a number based on the position of the current node in the source tree. IE, since <extra> is the first node in your input XML with the name "extra", it gets the number 1.

It sounds like you want the position of the <insert> node. And you want to number both <name> nodes and <insert> nodes, so you would do <xsl:number count="*"/>. To avoid counting the <extra> nodes you would do <xsl:number count="*[name()!='extra']"/>. However, in your example the extra nodes don't have the same parent as the <name> nodes, so this isn't an issue.

See also http://www.zvon.org/xxl/XSLTreference/Output/ (look up "number").

Bill

Denis Haskin wrote:

I sincerely apologize if this is a FAQ; I'm unable to find an answer to this in the archives nor the FAQ (although there are a couple of FAQ entries that are similar to this and imply to me that what I'm doing should work).

I am using xsl:key and select="key()" to include part of my input tree at another place during processing. This works fine, but the problem is I'm trying to using xsl:number as well and I can't prevent xsl:number from resetting (apparently) its context while processing the referred-to nodes. (Apologies if my terminology is not correct; I'm back to XSL after having not used it much in the last year-plus.)

Here's a *very* simplified example. Here's my input XML:

<test>
<extra myid="extraid"><name>Extra item</name></extra>
<item>
<name>Item one</name>
<name>Item two</name>
<name>Item three</name>
<insert ref="extraid"/>
</item>
</test>


and I'm processing it with this XSL:



<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:key name="testkey" match="extra" use="@myid"/>

<xsl:template match="insert">
 <xsl:variable name="d" select="key('testkey',@ref)"/>
 <xsl:apply-templates select="$d" mode="include"/>
</xsl:template>

<xsl:template match="name">
 <xsl:number/> - <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="extra" mode="include">
 <xsl:apply-templates/>
</xsl:template>

<xsl:template match="extra"/>

</xsl:stylesheet>

what I'm getting for output is this:

 1 - Item one
 2 - Item two
 3 - Item three
 1 - Extra item

and what I want is:

 1 - Item one
 2 - Item two
 3 - Item three
 4 - Extra item

Is this possible? I've messed a bunch with level, from, count, etc without any luck. I feel like this should be a no-brainer...

Much thanks --

dwh




XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list






XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords