Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


RE: List comparison

From: MukulGandhi@-----------.---------.---
To: NULL
Date: 5/9/2006 8:53:00 PM

If you want attribute ordering to be ignored also, you have to do

<xsl:for-each select="@*">
    <xsl:sort select="local-name()" />
    <xsl:value-of select="local-name()" />
    <xsl:value-of select="." />
</xsl:for-each>

Regards,
Mukul

"Mukul Gandhi" wrote:

> Hi Dmitry,
>    The stylesheet below will compare two lists. Ordering is ignored, and 
> duplicates are allowed and are ignored.
> 
> I am preparing string hash of two lists, and then comparing the hashes. 
> Also, node-set extension function is being used.
> 
> Please also note that this solution is not namespace aware.
> 
> The XSLT processor used is MSXSL 4.0.
> 
> The XML used is -
> 
> <lists>
>   <list1>
>     <item name="aaa"/>
>     <item name="bbb"/>
>   </list1>
>   <list2>
>     <item name="aaa"/>
>     <item name="bbb"/>
>     <item name="ccc"/>
>    </list2>
> </lists>
> 
> The stylesheet is -
> 
> <?xml version='1.0'?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>                        xmlns:msxsl="urn:schemas-microsoft-com:xslt"
>                        version="1.0">
> 
> <xsl:output method="text" encoding="UTF-8" />
> 
> <xsl:key name="dup" match="*" use="." />
> 
> <xsl:template match="/lists">
>    
>    <!-- process 1st list -->
>    <xsl:variable name="l" select="list1" />
>    
>    <xsl:variable name="x">
>      <xsl:for-each select="list1/*">          
>        <temp pos="{position()}">
>          <xsl:value-of select="local-name()" />
>          <xsl:for-each select="@*">
>            <xsl:value-of select="local-name()" />
>            <xsl:value-of select="." />
>          </xsl:for-each>
>        </temp>
>      </xsl:for-each>
>    </xsl:variable>
>    
>    <xsl:variable name="listA">
>      <xsl:for-each select="msxsl:node-set($x)/temp[generate-id() = 
> generate-id(key('dup', .)[1])]">
>        <xsl:sort select="." />
>        <xsl:variable name="pos" select="@pos" />
>        <xsl:copy-of select="$l/*[position() = $pos]" />
>      </xsl:for-each>
>    </xsl:variable>
>    
>    <xsl:variable name="hash1">
>      <xsl:for-each select="msxsl:node-set($listA)/*">
>        <xsl:value-of select="local-name()" />
>        <xsl:for-each select="@*">
>          <xsl:value-of select="local-name()" />
>          <xsl:value-of select="." />
>        </xsl:for-each>
>      </xsl:for-each>
>    </xsl:variable>
>    
>    <!-- process 2nd list -->
>    <xsl:variable name="m" select="list2" />
>    
>    <xsl:variable name="y">
>      <xsl:for-each select="list2/*">          
>        <temp pos="{position()}">
>          <xsl:value-of select="local-name()" />
>          <xsl:for-each select="@*">
>            <xsl:value-of select="local-name()" />
>            <xsl:value-of select="." />
>          </xsl:for-each>
>        </temp>
>      </xsl:for-each>
>    </xsl:variable>
>    
>    <xsl:variable name="listB">
>      <xsl:for-each select="msxsl:node-set($y)/temp[generate-id() = 
> generate-id(key('dup', .)[1])]">
>        <xsl:sort select="." />
>        <xsl:variable name="pos" select="@pos" />
>        <xsl:copy-of select="$m/*[position() = $pos]" />
>      </xsl:for-each>
>    </xsl:variable>
>    
>    <xsl:variable name="hash2">
>      <xsl:for-each select="msxsl:node-set($listB)/*">
>        <xsl:value-of select="local-name()" />
>        <xsl:for-each select="@*">
>          <xsl:value-of select="local-name()" />
>          <xsl:value-of select="." />
>        </xsl:for-each>
>      </xsl:for-each>
>    </xsl:variable>
>    
>    <!-- compare list1 and list2 -->
>    <xsl:choose>
>      <xsl:when test="$hash1 = $hash2">
>        <xsl:text>NO CHANGES WERE DETECTED</xsl:text>
>      </xsl:when>
>      <xsl:otherwise>
>        <xsl:text>CHANGES WERE DETECTED</xsl:text>
>      </xsl:otherwise>
>    </xsl:choose>
>    
> </xsl:template>
> 
> </xsl:stylesheet>
> 
> Regards,
> Mukul
> 
> "Dmitry Nogin" wrote:
> 
> > Hi,
> > 
> > How can I compare two lists?
> > Ordering should be ignored.
> > Duplicates are allowed and should be ignored.
> > 
> > For example:
> > 
> >    <list1>
> >      <item name="aaa"/>
> >      <item name="bbb"/>
> >    </list1>
> > 
> >    <list2>
> >      <item name="aaa"/>
> >      <item name="bbb"/>
> >      <item name="ccc"/>
> >    </list2>
> > 
> > I'd like to output something like this:
> > 
> >   CHANGES WERE DETECTED
> > 
> > or
> > 
> >   NO CHANGES  WERE DETECTED
> > 
> > Are there any simple approach?
> > 
> > 
> >     Thanks a lot,
> >          dmitry


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent