Re: [xsl] How to compare the attributes of two elements to ensure that they are equal

Subject: Re: [xsl] How to compare the attributes of two elements to ensure that they are equal
From: Mukul Gandhi <mukulgw3@xxxxxxxxx>
Date: Tue, 28 Oct 2003 20:40:25 -0800 (PST)
Hi Arvind,
 I tried to construct the XML, similar to the problem
description you have given. 

The XML, to which I applied the XSL is --
  <?xml version="1.0" encoding="UTF-8"?>
    <a id="1">one</a>
    <b id="2">two</b>
    <c id="1">three</c>
    <d id="4">four</d>
    <e id="5">five</e>

The XSL I have written is --

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
<xsl:output method="text" version="1.0"
encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
    <xsl:variable name="temp-RTF">
      <xsl:for-each select="*">
	<xsl:if test="name(.)= 'a' ">
	  <a>						    <xsl:for-each select="@*">
		  <xsl:value-of select="name(.)"/>
		  <xsl:value-of select="."/>
	      </att>					     </xsl:for-each>
	<xsl:if test="name(.)= 'c' ">
	    <xsl:for-each select="@*">
		    <xsl:value-of select="name(.)"/>
		    <xsl:value-of select="."/>
   <xsl:variable name= "a"
select="xalan:nodeset($temp-RTF)/a/*" />
   <xsl:variable name="c" 
select="xalan:nodeset($temp-RTF)/c/*" />
   <xsl:if test="$a = $c">
      Nodes 'a' and 'c' are equal (in terms of
   <xsl:if test="not($a = $c)">
      Nodes 'a' and 'c' are not equal (in terms of


The above XSL *works correctly for 1 attribute*
(Though, *the code iterates over all the attributes*).

I have randomly selected nodes 'a' and 'c' to compare
their attribute structures. 

I have also introduced additional tags for the sake of

The difficulty I am facing is, that order of
attributes is insignifacnt for the XML parsing.
*During one parsing instance*, attributes are returned
in a different order; in another parsing instance, the
same attribute list are returned in a different order.

I have a feeling, that the solution can be fully
solved using SET data structures(because within SET
structures, unordered data can be kept) .. *The
mathematical SET*, are implemented in the EXSLT
library.. **I hope, I am correct ;)**

Please see details about EXSLT at, ..


--- Arvind Bassi <arvind_bassi@xxxxxxxxxxx> wrote:
> What is the best way to compare two elements in
> terms of their
> attributes, and the attribute values? I need to
> ensure that each
> element has the same number of attributes, the same
> attribute names and
> the same attribute values.
> I am currently working on a stylesheet which
> compares two xml
> documents, and generates the differences between
> them. I have used Lar
> Huttar's xml document comparison stylesheet as my
> base (located at
>, but am stuck on
> augmenting it with checks on
> attributes.
> I am currently stuck with something along the lines
> of:
>    ...
>        <xsl:call-template name="compare-attributes">
>           with nodes as parameters
>    ...
>    <xsl:template name="compare-attributes">
>         <xsl:param name="attrA"/>
>         <xsl:param name="attrB"/>
>         <xsl:for-each select="$attrA/@*">
>                .... check that the same attribute
> exists with
>                .... the same attribute exists 
>                if it does then compare the values
> for equality
>         </xsl:for-each>
>     </xsl:template>
> However, l cannot get a handle on each of the two
> attributes to compare
> them. Can somebody help to fill in the blanks?
> Thanks.
> Arvind.
> Want to chat instantly with your online friends? 
> Get the FREE Yahoo!
> Messenger
>  XSL-List info and archive: 

Do you Yahoo!?
Exclusive Video Premiere - Britney Spears

 XSL-List info and archive:

Current Thread