Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


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

From: "Marrow" <m--a-r-r-o-w@--------------------.--->
To: NULL
Date: 8/5/2004 2:25:00 PM
Hi Yohem,

It would be far easier if you used ISO date formats (i.e. YYYY-MM-DD) -
anyway...

If you are trying to do basically an identity transform but sorting certain
elements - then start with an identity transform stylesheet and then amend
it to capture the exceptional situations.  And you might end up with
something like (which sorts your dates correctly)...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="month-order"
select="'JanFebMarAprMayJunJulAugSepOctNovDec'"/>
<xsl:output method="xml" indent="yes"/>

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

<xsl:template match="*[not(DENTALUNITS)]">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="*[DENTALUNITS]">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()[not(self::DENTALUNITS)]"/>
    <xsl:apply-templates select="DENTALUNITS">
      <xsl:sort select="substring(@date,8,2)" data-type="number"/>
      <xsl:sort
select="string-length(substring-before($month-order,substring(@date,4,3)))"
data-type="number"/>
      <xsl:sort select="substring(@date,1,2)" data-type="number"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="@* | text() | comment() | processing-instruction()">
  <xsl:copy/>
</xsl:template>

</xsl:stylesheet>


HTH
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator




"Yohem" <soydios@g...> wrote in message
news:839d42b7.0408031740.549d23b5@p......
> 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