[xsl] Numbering non-contiguous nodes

Subject: [xsl] Numbering non-contiguous nodes
From: yguaba@xxxxxxxxxxxx
Date: Wed, 03 Dec 2003 15:47:21 -0200
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 arms?</feedback>
		<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:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread