[xsl] Different parsers, different behaviour!

Subject: [xsl] Different parsers, different behaviour!
From: Richard Lewis <richard.lewis@xxxxxxxxx>
Date: Mon, 3 Nov 2003 14:58:06 +0000
Thanks very much for the links to command line XML (etc.) utilities.

I've installed and played with the XMLStarlet one (mainly because its not 
Java!)

However, something a bit weird is going on:

I have an XML document with the following structure:

<index>
	<item>
		<title>...</title>
		<author>...</author>
		<pages>nn</pages>
	</item>
	<item>
		<title>...</title>
		<author>...</author>
		<pages>nn</pages>
	</item>
	....
</index>

Its just an index of a bunch of pieces of sheet music in PDF format.  The 
'pages' element stores the number of pages each piece is presented on.

What I wanted to do was make a 'Contents' page for presenting the pieces in a 
booklet format.  It just lists the pieces in alphbetical order and calculates 
and displays which page they should start on.  I used two stylesheets; one 
just sorts the source document and passes its result to the second, which 
makes the contents page, and looks like this:

<xsl:stylesheet version="1.0" xmlns:xsl="...">

<xsl:output method="html" />

<xsl:template match="/">

<html>

<body>
	<h3>Collection</h3>
	<hr />
	<table border="0" cellspacing="3" width="100%">
		<xsl:apply-templates select="//item" />
	</table>
	<hr />
</body>

</html>

</xsl:template>

<xsl:template match="item">
	<tr>
		<td><xsl:value-of select="position()" /></td>
		<xsl:apply-templates />
	</tr>
</xsl:template>

<xsl:template match="title">
	<td><xsl:apply-templates /></td>
</xsl:template>

<xsl:template match="author">
	<td><xsl:apply-templates /></td>
</xsl:template>

<xsl:template match="pages">
	<td align="right"><xsl:value-of select="sum(preceding::pages) + 1" /></td>
</xsl:template>

</xsl:stylesheet>

Does this look like it should work? (particularly the match="pages" template)

It didn't work with the parser I normally use (Sablatron 1.0); it seems that 
'sum(preceding::pages)' was always 1 less that it should be.

But when I tried the stylesheet (unaltered) using XMLStarlet it produced 
exactly the correct output!

Whats going on?!

Cheers,
Richard

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


Current Thread