Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Sort xml and output xml

From: p.lepin@-------.---
To: NULL
Date: 12/6/2006 5:42:00 AM


Joe Kesselman wrote:
> Jim Andersen wrote:
> > As you can see from mr p.lepins posting the solution
> > is far from "trivial".

As Joe Kesselman said, it does a bit more than you asked
for. Partly because I wasn't really sure what you were
asking for, your original post being just a bit sloppy,
and partly because I tend to introduce what I perceive as
useful techniques/good practices when I post solutions to
problems on the usenet. Still, it's trivial enough even
then.

The transformation I posted copies any XML document fed to
it, sorting any siblings with sort attribute by the value
of that attribute (it breaks just a bit if there are
sibling elements without sort attribute interspersed with
elements it's trying to sort).

> I'm not sure I agree; that solution strikes me as
> overcomplicated for the question you asked.
>
> "Produce a new levelone document, copying the child
> elements in sorted order."
>
> <xsl:stylesheet version="1.0"
>    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>    <xsl:template select="/levelone">
>      <levelone>
>        <xsl:for-each select="child">
>          <xsl:sort select="@sort"/>
>          <xsl:copy-of select=".">
>        </xsl:apply-templates>
>      </levelone>
>    </xsl:template>
> </xsl:stylesheet>

Being my usual obnoxious self, I cannot resist the
temptation to point out that, in my opinion:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/levelone">
    <xsl:copy>
      <xsl:apply-templates>
        <xsl:sort select="@sort"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="child">
    <xsl:copy-of select="."/>
  </xsl:template>
</xsl:stylesheet>

is only a bit more complicated, and better from the
standpoint of teaching the Arguably Right Thing.

-- 
Pavel Lepin



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