Re: [xsl] Testing 2 XML documents for equality - a solution

Subject: Re: [xsl] Testing 2 XML documents for equality - a solution
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Wed, 30 Mar 2005 11:33:54 -0800 (PST)
Hi David,
  Below is a modified stylesheet.. I have solved few
of the bugs you pointed. I'll be happy if you can (or
others!) analyze this new stylesheet for any defects..

This version has these changes:
1) For the attribute nodes, storing also its element's
name and storing its *element's level*(as additional
safeguard - I am not quite sure if this safeguard is
really required. If it is'nt, then this is redundant
processing and can be removed).

2) For other nodes, also storing the level of node. 

<?xml version="1.0"?> 
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
 
 <xsl:output method="text" />  
 

 <!-- parameter for "ignoring white-space only text
nodes" during comparison -->
 <!-- if iws='y', "white-space only text nodes" will
not be considered during comparison  -->
 <xsl:param name="iws" />
 
 <xsl:variable name="doc1"
select="document('file1.xml')" />
 <xsl:variable name="doc2"
select="document('file2.xml')" />
 
 <xsl:template match="/">
 
    <!-- store hash of 1st document into a variable;
    it is concatination of name and values of all
nodes -->
    <xsl:variable name="one">
      <xsl:for-each select="$doc1//@*">
        <xsl:sort select="name()" />
        <xsl:value-of select="name()" />:<xsl:value-of
select="." />:<xsl:value-of select="name(..)"
/>:<xsl:value-of
select="count(../ancestor-or-self::node())" /> 
      </xsl:for-each>
      <xsl:choose>
        <xsl:when test="$iws='y'">
          <xsl:for-each
select="$doc1//node()[not(normalize-space(self::text())
= '')]">
            <xsl:value-of select="name()"
/>:<xsl:value-of select="." />:<xsl:value-of
select="count(ancestor-or-self::node())" /> 
          </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
          <xsl:for-each select="$doc1//node()">
	    <xsl:value-of select="name()" />:<xsl:value-of
select="." />:<xsl:value-of
select="count(ancestor-or-self::node())" /> 
          </xsl:for-each>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>  
    
    <!-- store hash of 2nd document into a variable;
    it is concatination of name and values of all
nodes -->
    <xsl:variable name="two">
      <xsl:for-each select="$doc2//@*">
        <xsl:sort select="name()" />
        <xsl:value-of select="name()" />:<xsl:value-of
select="." />:<xsl:value-of select="name(..)"
/>:<xsl:value-of
select="count(../ancestor-or-self::node())" /> 
      </xsl:for-each>
      <xsl:choose>
         <xsl:when test="$iws='y'">
           <xsl:for-each
select="$doc2//node()[not(normalize-space(self::text())
= '')]">
             <xsl:value-of select="name()"
/>:<xsl:value-of select="." />:<xsl:value-of
select="count(ancestor-or-self::node())" /> 
           </xsl:for-each>
         </xsl:when>
         <xsl:otherwise>
           <xsl:for-each select="$doc2//node()">
      	     <xsl:value-of select="name()"
/>:<xsl:value-of select="." />:<xsl:value-of
select="count(ancestor-or-self::node())" /> 
           </xsl:for-each>
         </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>  
    <xsl:choose>
      <xsl:when test="$one = $two">
        Equal
      </xsl:when>
      <xsl:otherwise>
        Not equal    
      </xsl:otherwise>
    </xsl:choose>
 </xsl:template>
 
</xsl:stylesheet>

Regards,
Mukul

--- David Carlisle <davidc@xxxxxxxxx> wrote:
> 
>   <xsl:for-each select="$doc1//@*">
>     <xsl:sort select="." />
> 
>   i.e. adding a xsl:sort instruction in the for-each
>   loop. This shall solve this problem!
> 
> No you need to sort on the attribute name, not it's
> value as the names
> are unique. You also need to distinguish attributes
> on different elemnts
> so you don't want //@*
> 
> David
> 
>
________________________________________________________________________
> This e-mail has been scanned for all viruses by
> Star. The
> service is powered by MessageLabs. For more
> information on a proactive
> anti-virus service working around the clock, around
> the globe, visit:
> http://www.star.net.uk
>
________________________________________________________________________
> 
> 


		
__________________________________ 
Do you Yahoo!? 
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/ 

Current Thread