By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

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

P: n/a
edw
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.
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a


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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output 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/

Jul 20 '05 #2

P: n/a
edw
Martin Honnen <Ma***********@t-online.de> wrote in message news:<3F**************@t-online.de>...
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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output 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 :-)
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.