RE: [xsl] Numbering non-contiguous nodes

Subject: RE: [xsl] Numbering non-contiguous nodes
From: bryan.s.schnabel@xxxxxxxxxxxxxx
Date: Wed, 3 Dec 2003 15:16:25 -0800

Unless your actual XML gets more complicated, with nested <gap>s, and so on,
you can just change this:

<xsl:value-of select="position()"/>

to this:

<xsl:value-of select="count(preceding-sibling::gap) + 1"/>

and get this:

is the largest fish on Earth. It feeds mainly on  

-----Original Message-----
From: yguaba@xxxxxxxxxxxx [mailto:yguaba@xxxxxxxxxxxx] 
Sent: Wednesday, December 03, 2003 9:47 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Numbering non-contiguous nodes

Hello all,

I'm looking for the simplest and most elegant solution to a node 
numbering problem. Below is an example of the relevant part of my XML 


		<value>blue whale</value>
		<feedback>Are whales really fish?</feedback>
		<value>giant squid</value>
		<feedback>Have you ever seen a fish with ten
		<value>whale shark</value>
		<feedback>Congratulations!</fee dback>
	<alt_feedback>Make sure you think of a FISH.</alt_feedback>
is the largest fish on Earth. It feeds mainly on  
		<value>human flesh</value>
		<feedback>Does the text say that all sharks are 
		<feedback>Are shrimp a kind of fish?</feedback>
		<feedback>All right!</feedback>
	<alt_feedback>Which other animal are whale sharks compared 


As you can see, each "question" element may contain text and any 
number of "gap" elements. Now, basically, I need my output to be like 


<p>The [_1_] is the largest fish on Earth. It feeds mainly on  

In other words, all text nodes should appear in the output, but the 
"gap" element nodes should be replaced with "[_" + gap number + "_]", 
where "gap number" is the sequential number of each "gap" element.

I thought of using XSLT code along these lines:


<xsl:template match="question">
	<xsl:for-each select="node()">
			<xsl:when test="local-name() = 'gap'">
				<xsl:value-of select="position()"/>
				<xsl:value-of select="."/>


The problem is that I only want the "gap" nodes to be counted, but 
the above solution counts all child nodes of "question" (including 
the text nodes), so the numbering is incorrect for my purposes. I 
thought of using a variable as a counter that would be incremented 
each time <xsl:when test="local-name() = 'gap'"> evaluated to true, 
but after considerable research have come to the [possibly 
inaccurate] conclusion that XSL variables cannot be reassigned values 
this way.

Any and all suggestions will be greatly appreciated.


 XSL-List info and archive:

 XSL-List info and archive:

Current Thread