Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > How to sort a XML file itself or how to compare two XML files whith pretty printed diff.

Reply
Thread Tools

How to sort a XML file itself or how to compare two XML files whith pretty printed diff.

 
 
edw
Guest
Posts: n/a
 
      07-03-2003
All,

I got following question. I want to sort a XML files content in
following manner:

Source file:
<btag>
<bbtag key=val/>
<abtag key=val/>
</btag>
<atag>
</atag>
<btag>
<bbtag key=val/>
<abtag key=val/>
<cbtag key=val/>
</btag>


Target file:
<atag>
</atag>
<btag>
<abtag key=val/>
<bbtag key=val/>
</btag>
<btag>
<abtag key=val/>
<bbtag key=val/>
<cbtag key=val/>
</btag>

As order in my XML structures do not matter, the sorting my be based
on ASCII or a MD5 sum of the substructure.

This is just to feed it into a pretty printing text diff tool, that
compares several 10.000 XML files...

So any idea on sorting my sourcefiles (so I still can use my backend
i'm quite happy with) or a different backend that compares and makes a
pretty HTML output file.

Thanx in advance
edw.
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      07-04-2003


edw wrote:
> All,
>
> I got following question. I want to sort a XML files content in
> following manner:
>
> Source file:
> <btag>
> <bbtag key=val/>
> <abtag key=val/>
> </btag>
> <atag>
> </atag>
> <btag>
> <bbtag key=val/>
> <abtag key=val/>
> <cbtag key=val/>
> </btag>


That example is not well-formed XML, there is no root element and the
attribute values are not properly quoted.
With the following example

<?xml version="1.0" encoding="UTF-8"?>
<root>
<btag>
<bbtag key="val"/>
<abtag key="val"/>
</btag>
<atag>
</atag>
<btag>
<bbtag key="val"/>
<abtag key="val"/>
<cbtag key="val"/>
</btag>
</root>

and the following XSLT stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">

<xslutput method="xml" indent="yes" />

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

<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="*">
<xsl:sort order="ascending" data-type="text" select="local-name()" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

which sorts elements according to the local-name I get

<?xml version="1.0" encoding="UTF-8"?>
<root>
<atag/>
<btag>
<abtag key="val"/>
<bbtag key="val"/>
</btag>
<btag>
<abtag key="val"/>
<bbtag key="val"/>
<cbtag key="val"/>
</btag>
</root>
--

Martin Honnen
http://JavaScript.FAQTs.com/

 
Reply With Quote
 
 
 
 
edw
Guest
Posts: n/a
 
      07-09-2003
Martin Honnen <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> edw wrote:
> > All,
> >
> > I got following question. I want to sort a XML files content in
> > following manner:
> >
> > Source file:
> > <btag>
> > <bbtag key=val/>
> > <abtag key=val/>
> > </btag>
> > <atag>
> > </atag>
> > <btag>
> > <bbtag key=val/>
> > <abtag key=val/>
> > <cbtag key=val/>
> > </btag>

>
> That example is not well-formed XML, there is no root element and the
> attribute values are not properly quoted.
> With the following example
>
> <?xml version="1.0" encoding="UTF-8"?>
> <root>
> <btag>
> <bbtag key="val"/>
> <abtag key="val"/>
> </btag>
> <atag>
> </atag>
> <btag>
> <bbtag key="val"/>
> <abtag key="val"/>
> <cbtag key="val"/>
> </btag>
> </root>
>
> and the following XSLT stylesheet
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlnssl="http://www.w3.org/1999/XSL/Transform">
>
> <xslutput method="xml" indent="yes" />
>
> <xsl:template match="@* | /">
> <xsl:copy>
> <xsl:apply-templates select="@* | node()" />
> </xsl:copy>
> </xsl:template>
>
> <xsl:template match="*">
> <xsl:copy>
> <xsl:apply-templates select="@*" />
> <xsl:apply-templates select="*">
> <xsl:sort order="ascending" data-type="text" select="local-name()" />
> </xsl:apply-templates>
> </xsl:copy>
> </xsl:template>
>
> </xsl:stylesheet>
>
> which sorts elements according to the local-name I get
>
> <?xml version="1.0" encoding="UTF-8"?>
> <root>
> <atag/>
> <btag>
> <abtag key="val"/>
> <bbtag key="val"/>
> </btag>
> <btag>
> <abtag key="val"/>
> <bbtag key="val"/>
> <cbtag key="val"/>
> </btag>
> </root>


Ahh. Interesting solution. Thanx a lot.
edw, headding for a XSL manual
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
Pretty-up scans of offset printed materials David Arnstein Digital Photography 5 10-11-2005 01:24 AM
Re: One more Pretty Face - Pretty face9.jpg [1/1] Harvey Digital Photography 7 11-04-2004 09:53 AM
Re: Pretty Please Peer2Peer need help Pretty Please John Haithwaite @ Blue Case Solutions Computer Support 0 07-06-2003 10:20 PM
Re: Pretty Please Peer2Peer need help Pretty Please Ralph Wade Phillips Computer Support 0 07-06-2003 09:07 PM



Advertisments