472,333 Members | 1,112 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,333 software developers and data experts.

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

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
2 9676


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lad | last post by:
Hi, What is the best method for comparing two files by words? I was thinking about reading files by words and compare them but a word in one file...
2
by: sudheervemana | last post by:
Dear all, In my main directory there are some source files and i have another directory which includes several folders,each contains the make...
8
by: pjsimon | last post by:
I want to compare two files like MS Visual SourceSafe's Show Differences feature. Is there a way to access methods in VB.Net that will let me use...
2
by: Rocker | last post by:
hey, i wanna know if there's any method in VB to save all data in an exe file as they are in the exe file itself without saving them to a text...
3
by: shona | last post by:
Hi, can any one told me how to compare files with same name but different extension.. for eg. if a.txt & a.doc then ans is same files.....
4
by: Clay Hobbs | last post by:
I am making a program that (with urllib) that downloads two jpeg files and, if they are different, displays the new one. I need to find a way to...
0
by: zw | last post by:
Hi I have 2 log files, each with a timestamp on the first 2 fields. However, when I do a awk '/ / {print $1,$2}' logs/x.log on a log file, ...
0
by: Arulmanoj | last post by:
Hi, I need a batch file to delete all PDF files in a folder and it's subfolders. I will be giving the folder path and number of old days to delete...
3
by: Susan StLouis | last post by:
I'm writing a program that can be used to compare files. The program features a select that contains a list of files. After selecting several of the...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.