[xsl] xsl:sort with document()

Subject: [xsl] xsl:sort with document()
From: Jan Limpens <jan.limpens@xxxxxxxxx>
Date: Thu, 2 Dec 2004 16:06:07 -0200
hi,

I have the content of a website organized in folders, so there is a
one webpage to one xml-file relation. I read the contents of the files
and generate a xml document from this.
In a second step I use a xslt stylesheet to open each of this files
via document() and extract some information, which leads to a
navigation structure, where I have a link, a title, a description and
so on.
now my problem: as the files are in the filesystem, they are ordered
by alphabet (at least I get it this way from the .net framework) which
is not what I want.  For example I want the page  'contact' to be the
last of a generation, but as it starts with c it comes somewhere at
the beginning.
so I had no better idea than put a navi:order field into each of these
files, which contains a number after which I could sort. (anyone with
a better idea? I don't like mine too much because it mixes structure
with content).

Anyway, I have not found a way to actually bring this to work...

some examples ....

the filestructure...
<?xml version="1.0"?>
<site xmlns:site="http://limpens.com/site";>
	<site:document>
		<site:xml-file>file:///D:/XmlContent/Home.xml</site:xml-file>
		<site:children>
			<site:document>
				<site:xml-file>file:///D:/XmlContent/Home/Development.xml</site:xml-file>
				<site:children>
					<site:document>
						<site:xml-file>file:///D:/XmlContent/Home/Development/TaskSheet.xml</si
te:xml-file>
					</site:document>
					<site:document>
						<site:xml-file>file:///D:/XmlContent/Home/Development/VectorPainter.xml
</site:xml-file>
					</site:document>
					<site:document>
						<site:xml-file>file:///D:/XmlContent/Home/Development/WwwLimpensCom.xml
</site:xml-file>
					</site:document>
					<site:document>
						<site:xml-file>file:///D:/XmlContent/Home/Development/WwwWaldeckAt.xml<
/site:xml-file>
					</site:document>
				</site:children>
			</site:document>
			<site:document>
				<site:xml-file>file:///D:/XmlContent/Home/Illustrations.xml</site:xml-fil
e>
			</site:document>
		</site:children>
	</site:document>
</site>

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

the xslt to produce the navigation structure

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:site="http://limpens.com/site";
xmlns:blog="http://limpens.com/blogbase";
xmlns:navi="http://limpens.com/navigation";>
	<xsl:output method="xml" encoding="UTF-16" indent="yes"/>
	<xsl:param name="selectedLanguage" select="'de'"/>
	<xsl:param name="defaultLanguage" select="'en'"/>
	<xsl:param name="baseDir" select="'file:///D:/XmlContent'"/>

	<xsl:template match="site">
		<site>
			<xsl:apply-templates select="site:document"/>
		</site>
	</xsl:template>

	<xsl:template match="site:document">
		<site:document>
			<site:xml-file>
				<xsl:value-of select="site:xml-file"/>
			</site:xml-file>
			<site:link>
				<xsl:text>/</xsl:text>
				<xsl:value-of select="$selectedLanguage"/>
				<xsl:value-of
select="substring-before(substring-after(string(site:xml-file),
$baseDir), 'xml')"/>
				<xsl:text>aspx</xsl:text>
			</site:link>
			<xsl:apply-templates select="document(site:xml-file)"/>
			<xsl:if test="site:children">
				<site:children>
					<xsl:apply-templates select="site:children/site:document">
						<xsl:sort select="navi:order"/><!--does not work-->
					</xsl:apply-templates>
				</site:children>
			</xsl:if>
		</site:document>
	</xsl:template>

	<<xsl:template match="blog:Blog">
		<site:title>
			<xsl:value-of
select="blog:BlogInfos/blog:BlogInfo[lang($selectedLanguage)]/navi:title"/>
		</site:title>
		<site:description>
			<xsl:value-of
select="blog:BlogInfos/blog:BlogInfo[lang($selectedLanguage)]/blog:Descriptio
n"/>
		</site:description>
		<site:order>
			<xsl:value-of select="navi:order"/>
		</site:order>
	</xsl:template>

</xsl:stylesheet>
======================================================================

a sample xml document

<?xml version="1.0" ?>
<Blog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xmlns="http://limpens.com/blogbase";
	xsi:schemaLocation="http://limpens.com/blogbase Schemas\BlogBase.xsd"
xmlns:navi="http://limpens.com/navigation";>
	<navi:order>30</navi:order>
	<BlogInfos>
		<BlogInfo xml:lang="en">
			<navi:title>Software Development</navi:title>
			<Title>Diverse Software Development Projects</Title>
			<Description>Once in a while I still reach back to my other passion
- programming. Sometimes something usefull emerges, then you can read
about it just here...</Description>
		</BlogInfo>
		<BlogInfo xml:lang="de">
			<navi:title>Software Entwicklung</navi:title>
			<Title>Verschiedene Software Entwicklungen</Title>
			<Description>Hin und wieder gebe ich mich meiner anderen
Leidenschaft (neben dem Zeichnen) hin - der Programmierung. Manchmal
entstehen dabei auch n|tzliche und/oder interessante
Dinge.</Description>
		</BlogInfo>
		<BlogInfo xml:lang="pt">
			<navi:title>Desenvolvimento de Soft</navi:title>
			<Title>Diversas desenvolvimentos de soft</Title>
			<Description>De vez em quando eu crio algo pratico nessa areo. I
aqui onde eu escevo sobre estes assuntos...</Description>
		</BlogInfo>
	</BlogInfos>
</Blog>

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

the navigation structure

<?xml version="1.0"?>
<site xmlns:site="http://limpens.com/site";
xmlns:blog="http://limpens.com/blogbase";
xmlns:navi="http://limpens.com/navigation";>
	<site:document>
		<site:xml-file>file:///D:/XmlContent/Home.xml</site:xml-file>
		<site:link>/en/Home.aspx</site:link>
		<site:title>Homepage</site:title>
		<site:description>Here you'll find a selection of my works in the
areas of illustration, my recent publications and some articles on
software design.</site:description>
		<site:children>

			<site:document>
				<site:xml-file>file:///D:/XmlContent/Home/Development.xml</site:xml-file>
				<site:link>/en/Home/Development.aspx</site:link>
				<site:title>Software Development</site:title>
				<site:description>Once in a while ...</site:description>
				<site:order>30</site:order>

				<site:children>
					<site:document>
						<site:xml-file>file:///D:/XmlContent/Home/Development/TaskSheet.xml</si
te:xml-file>
						<site:link>/en/Home/Development/TaskSheet.aspx</site:link>
						<site:title>Task Sheet - an online time management
software</site:title>
						<site:description>TaskSheet</site:description>
						<site:thumbnail>/Images/Thumbnails/TaskSheet.png</site:thumbnail>
					</site:document>
					<site:document>
						<site:xml-file>file:///D:/XmlContent/Home/Development/VectorPainter.xml
</site:xml-file>
						<site:link>/en/Home/Development/VectorPainter.aspx</site:link>
						<site:title>A vector drawing application still in planning</site:title>
						<site:description>This shows...</site:description>
					</site:document>
					<site:document>
						<site:xml-file>file:///D:/XmlContent/Home/Development/WwwLimpensCom.xml
</site:xml-file>
						<site:link>/en/Home/Development/WwwLimpensCom.aspx</site:link>
						<site:title>www.limpens.com - new and shiney</site:title>
						<site:description>There was an old one, ...</site:description>
					</site:document>

					<site:document>
						<site:xml-file>file:///D:/XmlContent/Home/Development/WwwWaldeckAt.xml<
/site:xml-file>
						<site:link>/en/Home/Development/WwwWaldeckAt.aspx</site:link>
						<site:title>Waldeck.at gets a new face (well, a new
technology)</site:title>
						<site:description>With technologies similar ...</site:description>
					</site:document>
				</site:children>
			</site:document>
			<site:document>
				<site:xml-file>file:///D:/XmlContent/Home/Illustrations.xml</site:xml-fil
e>
				<site:link>/en/Home/Illustrations.aspx</site:link>
				<site:title>Illustration</site:title>
				<site:description>Here you will find a wild mix of projects, jobs,
ideas, rants and everything that comes to my mind in this
category.</site:description>
				<site:order>10</site:order>
			</site:document>
		</site:children>
	</site:document>
</site>

any idea how to sort after navi:order?

many thanks!

--
jan

Current Thread