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

Re: Comparing 2 XML Documents

Subject: Re: Comparing 2 XML Documents
From: Ragulf Pickaxe <ragulf.pickaxe@xxxxxxxxx>
Date: Wed, 9 Nov 2005 14:59:29 +0100
compare 2 xml with xslt
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

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.