473,396 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 9724


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 can be linked with a new line character ( \n) and...
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 files.Now i want to debug my source code in either...
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 existing MS code to show the differences between...
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 file or something else
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.. Thanks
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 compare two files in Python. How is this done? ...
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, it is complicated by the fact that I also get other...
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 as static values.. Please it's very urgent. Also...
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 files. a "Biggest" button can be pushed to find...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.