Re: [xsl] Simple XML Diff

Subject: Re: [xsl] Simple XML Diff
From: Michael Ludwig <mlu@xxxxxxxxxxxxx>
Date: Fri, 11 Jul 2008 10:07:46 +0200
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"

<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:when test=". = $melm">=</xsl:when>
      <!-- Attribute -->
      <xsl:for-each select="@*">
        <xsl:sort select="name()"/>
        <xsl:value-of select="concat( '@', name(), ': ')"/>
        <xsl:variable name="name" select="name()"/>
          <xsl:when test=". = $melm/@*[ name() = $name ]">=</xsl:when>


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

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

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

Michael Ludwig

Current Thread