[xsl] Sort by page number

Subject: [xsl] Sort by page number
From: Jostein Austvik Jacobsen <josteinaj@xxxxxxxxx>
Date: Wed, 21 Oct 2009 17:29:04 +0200
I've got a stylesheet that merges XML-files specified in the input
document. Its pretty short so I'll just post the whole thing here:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/>
	<xsl:variable name="docs" select="/*/doc"/>
	
	<xsl:template match="mergeDocs">
		<xsl:apply-templates select="doc[1]"/>
	</xsl:template>
	
	<xsl:template match="doc">
		<xsl:variable name="path" select="@path"/>
		<xsl:for-each select="document($path)/*">
			<xsl:sort select=".//@page[1]"/>
			<xsl:copy>
				<article>
				<xsl:copy-of select="@* | *"/>
				</article>
				<xsl:for-each select="$docs[position() &gt; 1]">
					<article>
						<xsl:copy-of select="document(@path)/*/*"/>
					</article>
				</xsl:for-each>
			</xsl:copy>
		</xsl:for-each>
	</xsl:template>
	
</xsl:stylesheet>

The input file can look like this:

<mergeDocs>
  <doc path="doc1.xml"/>
  <doc path="doc2.xml"/>
  <doc path="doc3.xml"/>
</mergeDocs>

As you might figure out from the stylesheet, each of these XML-files
contains an article. There are no metadata available about the
ordering of these articles besides that they usually (not all do, so
"usually") contain a pagebreak tag like this one:

<pagebreak page="36"/>

Lets assume that these pagebreaks can occur at any place within the
article. In the stylesheet above I've tried to sort the articles like
this:

<xsl:sort select=".//@page[1]"/>

In my head this XPath expression reads "in the current article -> go
through all of the descendants -> select all of their attributes ->
pick out the page attributes -> pick the first one".

However, when run; nothing is sorted - as if the xsl:sort wasn't there
at all. I suspect that maybe it searches through the entire document
each time so that all articles are sorted as "page 1"? What am I doing
wrong here...

Regards
Jostein

Current Thread