Re: [xsl] Sorting an xml

Subject: Re: [xsl] Sorting an xml
From: Jimmy Åhs <jimmyahs@xxxxxxxxx>
Date: Tue, 23 May 2006 11:45:38 +0200
Tank you for your answer. This sorts my XML as I asked. But what I realy wanted (I was not realy clear about this)
was a HTML output like below. All nodes with the same tag <place> should be printed in a group, with the <place> as heading.


Also thank you for the link to Jeni Tennison touturials, its a great page. I tryed to make a code from that but I cant get it to work,
and I'm new to this SXL thing. My XML- and XSL-file looks like this at the moment:


--------vvvvvv XML vvvvvv---------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="shopinglist.xsl"?>
<shopinglist>
<item>
<name>Lax</name>
<count>8</count>
<unit>st</unit>
<place>Kyl och frys</place>
</item>
<item>
<name>Chalottenlvk</name>
<count>4</count>
<unit>st</unit>
<place>Frukt och grvnt</place>
</item>
<item>
<name>Matlagningsvin</name>
<count>0,66</count>
<unit>l</unit>
<place>Vvrigt</place>
</item>
<item>
<name>Grddde</name>
<count>1,2</count>
<unit>l</unit>
<place>Kyl och frys</place>
</item>
</shopinglist>
--------^^^^^ XML ^^^^^---------------------------------------------------------------------------------------------
--------vvvvvv XSL vvvvvv---------------------------------------------------------------------------------------------
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns="http://www.w3.org/1999/xhtml";>
<body style="font-family:Arial,helvetica,sans-serif;font-size:12pt;background-color:#EEEEEE">
<xsl:key name="item-by-place" match="item" use="place" />
<xsl:template match="shopinglist">
<xsl:for-each select="item[count(. | key('item-by-place', place)[1]) = 1]">
<xsl:sort select="place" />
<xsl:value-of select="place" />,<br />
<xsl:for-each select="key('item-by-place', place)">
<xsl:sort select="name" />
<xsl:value-of select="name" /> (<xsl:value-of select="count" /> <xsl:value-of select="unit" />)<br />
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</body>
</html>
--------^^^^^ XSL ^^^^^---------------------------------------------------------------------------------------------
--------vvvvvv wanted output vvvvv-------------------------------------------------------------------------------------


Kyl och frys:
Lax 8 st
Grddde 1,2 l

Frukt och grvnt:
Chalottenlvk 4 st

Vvrigt:
Matlagningsvin 0,66  l

Jagdishwar B wrote:

----------------------------------------------------------

The printout for this should be sorted by <place> like this:
Kyl och frys:
Lax 8 st
Grddde 1,2 l

Frukt och grvnt:
Chalottenlvk 4 st

Vvrigt:
Matlagningsvin 0,66  l

It would be easier to understand if you give what output xml you are
looking for.

you can try:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<!-- default identity translation -->
<xsl:template match="*|@*|comment()|processing-instruction()|text()">
<xsl:copy>
<xsl:apply-templates
select="*|@*|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
<!-- start processing with the root element -->
<xsl:template match="shopinglist">
<xsl:copy>
<xsl:apply-templates select="item">
<xsl:sort select="place"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


It will reorder the source xml item elements according to the "place"

Current Thread