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

Discussion in 'XML' started by edw, Jul 3, 2003.

  1. edw

    edw Guest

    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.
    edw, Jul 3, 2003
    #1
    1. Advertising

  2. Re: How to sort a XML file itself or how to compare two XML fileswhith pretty printed diff.

    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: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>
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jul 4, 2003
    #2
    1. Advertising

  3. edw

    edw Guest

    Martin Honnen <> wrote in message news:<>...
    > 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: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 :)
    edw, Jul 9, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cyril Vi?ville

    diff Process under diff users

    Cyril Vi?ville, Jun 29, 2004, in forum: Perl
    Replies:
    1
    Views:
    497
    Joe Smith
    Jun 29, 2004
  2. Berrucho
    Replies:
    2
    Views:
    617
    Infant Newbie
    Dec 5, 2003
  3. A Traveler

    Diff CSS styles for diff INPUT TYPE='s?

    A Traveler, Aug 31, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,902
    Steve Fulton
    Aug 31, 2004
  4. GenxLogic
    Replies:
    3
    Views:
    1,238
    andrewmcdonagh
    Dec 6, 2006
  5. Austin Ziegler

    [ANN] Diff::LCS 1.1.0, Diff::LCS 1.0.4

    Austin Ziegler, Aug 8, 2004, in forum: Ruby
    Replies:
    3
    Views:
    176
    Austin Ziegler
    Aug 9, 2004
Loading...

Share This Page