Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Help! How do I sort my data, and keep the root's attributes at the same time?

From: soydios@-----.--- (-----)
To: NULL
Date: 8/3/2004 6:41:00 PM
I'm trying to sort my XML which looks like:

<?xml version="1.0" encoding="UTF-16"?>
<LABORATORY case="234" project="TR43" Type="A2">
   <CLIENT Name="XXXX" LName="XXXX"/>
   <CLIENT Name="WWWW" LName="WWWW"/>
   <CLIENT Name="QQQQ" LName="QQQQ"/>
   <CLIENT Name="RRRR" LName="RRRR"/>
   <CLIENT Name="TTTT" LName="TTTT"/>
   <CLIENT Name="DDDD" LName="DDDD"/>
   <DENTALUNITS Number="4" date="10/May/04">DATA GOES
HERE</DENTALUNITS>
   <DENTALUNITS Number="4" date="14/Apr/04">DATA GOES
HERE</DENTALUNITS>
   <DENTALUNITS Number="4" date="17/Jun/04">DATA GOES
HERE</DENTALUNITS>
   <DENTALUNITS Number="4" date="20/Jan/04">DATA GOES
HERE</DENTALUNITS>
   <DENTALUNITS Number="4" date="23/Mar/04">DATA GOES
HERE</DENTALUNITS>
   <DENTALUNITS Number="4" date="29/Dec/04">DATA GOES
HERE</DENTALUNITS>
</LABORATORY>

The problem is that when I try to sort my data, I end up with the same
XML as the source.

I suspect this is due to the copy-of select="@*|."/ I'm using, which
is copying the whole tree. So I tried using xsl:copy to only copy the
root, and then sort the child, but that didn't work either. This is my
XSL:

<?xml version='1.0'?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="LABORATORY|LABORATORY/CLIENT">
<xsl:copy>
<xsl:copy-of select="@*|."/>

<xsl:apply-templates select="LABORATORY/DENTALUNITS">
<xsl:sort select="substring(@date,8,2)" data-type="number"
order="ascending"/> <xsl:sort select="substring(@date,4,3)"
data-type="text" order="ascending"/> <xsl:sort
select="substring(@date,1,2)" data-type="number" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="LABORATORY/DENTALUNITS">
	<xsl:copy-of select="@*|."/>
</xsl:template>
	
</xsl:transform>

All I need to do is to copy the CLIENT data as it is, and to sort the
DENTALUNITS data by date. I have tried different approaches to do
this, but when I get the attributes of LABORATORY nothing else is
done. I get someting like:

<?xml version="1.0" encoding="UTF-16"?>
<LABORATORY case="234" project="TR43" Type="A2">
</LABORATORY>

This is the XSL I used in this case:


<?xml version='1.0'?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<xsl:for-each select="LABORATORY">
<LABORATORY case="{@case}" project="{@project}" Type="{@Type}">

<xsl:apply-templates select="LABORATORY/CLIENT">
</xsl:apply-templates>		

<xsl:apply-templates select="LABORATORY/DENTALUNITS">
<xsl:sort select="substring(@date,8,2)" data-type="number"
order="ascending"/>
<xsl:sort select="substring(@date,4,3)" data-type="text"
order="ascending"/>
<xsl:sort select="substring(@date,1,2)" data-type="number"
order="ascending"/>
</xsl:apply-templates>
</LABORATORY>

</xsl:for-each>
</xsl:template>

<xsl:template match="LABORATORY/CLIENT">
	<xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="LABORATORY/DENTALUNITS">
	<xsl:copy-of select="."/>
</xsl:template>
	
</xsl:transform>


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent