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
Erik,

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:

The 
[_1_]
is the largest fish on Earth. It feeds mainly on  
[_2_]
.

-----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 
code:

===========
XML SNIPPET
*****************

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

===========

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 
this:

===============
DESIRED OUTPUT
**********************

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

===============
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:

==============
PROPOSED XSLT
*********************

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

==============

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.

Erik

 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