xsl-list
[Top] [All Lists]

Re: Comparing 2 XML Documents

2005-11-09 06:59:29
Hi Fraser,


On 11/9/05, Fraser Goffin <goffinf(_at_)hotmail(_dot_)com> 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 :-)

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