xsl-list
[Top] [All Lists]

Re: [xsl] Simple XML Diff

2008-07-11 01:08:21
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(_at_)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(_at_)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(_at_)forelle:~/Werkstatt/xsl > xsltproc Diff.xsl A.xml
Urmel : !
@u: =
eins : =
zwei : =
drei : !
vier : =
@v: !
x:fuenf : =

Michael Ludwig

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>