Re: [xsl] Comparing 2 XML Documents

Subject: Re: [xsl] Comparing 2 XML Documents
From: Ragulf Pickaxe <ragulf.pickaxe@xxxxxxxxx>
Date: Wed, 9 Nov 2005 14:59:29 +0100
Hi Fraser,


On 11/9/05, Fraser Goffin <goffinf@xxxxxxxxxxx> wrote:
> Does anyone know a good way of comparing aspects of two XML instance
> documents using XSLT. The number of possible differences may be large so I
> would prefer not to have to create a hard coded test for each

Instead of explicitly testing for all possible occurences, I would
compare two documents in a different way.

I would create two variables:
<xsl:variable name="Doc1" select="/*"/>
<xsl:variable name="Doc2" select="document('foo.xml')/*"/>

Then go recursively through the elements first through $Doc1,
comparing this to $Doc2, then through $Doc2 comparing to $Doc1.

I have written a very scetchy (and unfinished) part of what I would
do, here. Perhaps you can use it, perhaps not.

<xsl:template match="/">
  <xsl:apply-templates select="$Doc1" mode="Doc1-vs-Doc2"/>
  <xsl:apply-templates select="$Doc1" mode="Doc2-vs-Doc1"/>
</xsl:template>

<xsl:template match="*" mode="Doc1-vs-Doc2">
  <xsl:param name="doc2" select="$Doc2"/>
  <xsl:choose>
    <xsl:when test="local-name()!=local-name($doc2)">
      <xsl:apply-templates select="@*" mode="Doc1-vs-Doc2">
        <xsl:with-param name="doc2" select="$doc2"/>
      </xsl:apply-templates>
      <xsl:for-each select="*">
        <xsl:variable name="pos" select="position()"/>
        <xsl:apply-temlates select="." mode="Doc1-vs-Doc2">
          <xsl:with-param name="doc2" select="$doc2/*[position()=$pos]"/>
        </xsl:apply-templates>
      </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
      <Mismatch Type="Doc1-vs-Doc2">
        <NotExist><xsl:value-of select="local-name()"/></NotExist>
      </Mismatch>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Regards,
Ragulf Pickaxe :-)

Current Thread