[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Simple XML Diff

Subject: Re: Simple XML Diff
From: Michael Ludwig <mlu@xxxxxxxxxxxxx>
Date: Fri, 11 Jul 2008 10:07:46 +0200
Re:  Simple XML Diff
Mark Anderson schrieb:
I'm trying to write an XML template that will compare two XML files
that have *identical* structures.

If you can guarantee identical structure, elements can be compared based on their position in the document, and attributes can be compared based on the element they belong to and their name.

What I need to do is compare every element an attribute in new.xml
with the equivalent in master.xml [...] What I'm stuck with is [...]
how to iterate through all elements and attributes in new.xml and find
the equivalent in master.xml.

Maybe this is a first step you can start working from:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/>

  <xsl:variable name="master" select="document('master.xml')"/>
  <xsl:variable name="master-elms" select="$master//*"/>

  <xsl:template match="/">
    <xsl:for-each select="//*">
      <xsl:variable name="melm" select="$master-elms[ position() ]"/>
      <!-- Text -->
      <xsl:value-of select="concat( name(), ' : ')"/>
      <xsl:choose>
        <xsl:when test=". = $melm">=</xsl:when>
        <xsl:otherwise>!</xsl:otherwise>
      </xsl:choose>
      <xsl:text>&#10;</xsl:text>
      <!-- Attribute -->
      <xsl:for-each select="@*">
        <xsl:sort select="name()"/>
        <xsl:value-of select="concat( '@', name(), ': ')"/>
        <xsl:variable name="name" select="name()"/>
        <xsl:choose>
          <xsl:when test=". = $melm/@*[ name() = $name ]">=</xsl:when>
          <xsl:otherwise>!</xsl:otherwise>
        </xsl:choose>
        <xsl:text>&#10;</xsl:text>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>

</xsl:transform>

mludwig@forelle:~/Werkstatt/xsl > cat master.xml
<Urmel u="U">
        <eins>eins</eins>
        <zwei>zwei</zwei>
        <drei>drei</drei>
        <vier v="V">vier</vier>
        <x:fuenf xmlns:x="bla">fuenf</x:fuenf>
</Urmel>

mludwig@forelle:~/Werkstatt/xsl > cat A.xml
<Urmel u="U">
        <eins>eins</eins>
        <zwei>zwei</zwei>
        <drei>dreiunddrei_ig</drei>
        <vier v="W">vier</vier>
        <x:fuenf xmlns:x="bla">fuenf</x:fuenf>
</Urmel>

mludwig@forelle:~/Werkstatt/xsl > xsltproc Diff.xsl A.xml
Urmel : !
@u: =
eins : =
zwei : =
drei : !
vier : =
@v: !
x:fuenf : =

Michael Ludwig

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.