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

E

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.
 
M

Martin Honnen

edw said:
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:eek:utput 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>
 
E

edw

Martin Honnen said:
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:eek:utput 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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top