xsl-list
[Top] [All Lists]

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

2003-10-28 21:40:25
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"?>
  <root>
    <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>
  </root>

The XSL I have written is --

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:xalan="http://xml.apache.org/xalan";>
<xsl:output method="text" version="1.0"
encoding="UTF-8" indent="yes"/>
        
<xsl:template match="/root">
  <root>
    <xsl:variable name="temp-RTF">
      <xsl:for-each select="*">
        <xsl:if test="name(.)= 'a' ">
          <a>                                               <xsl:for-each 
select="@*">
              <att>
                <name>
                  <xsl:value-of select="name(.)"/>
                </name>
                <value>
                  <xsl:value-of select="."/>
                </value>
              </att>                                         </xsl:for-each>
          </a>
        </xsl:if>
        <xsl:if test="name(.)= 'c' ">
          <c>
            <xsl:for-each select="@*">
              <att>
                 <name>
                    <xsl:value-of select="name(.)"/>
                 </name>
                 <value>
                    <xsl:value-of select="."/>
                 </value>
              </att>
            </xsl:for-each>
           </c>
        </xsl:if>
   </xsl:for-each>
   
   </xsl:variable>
                        
   <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
attributes)
   </xsl:if>
   <xsl:if test="not($a = $c)">
      Nodes 'a' and 'c' are not equal (in terms of
attributes)
   </xsl:if>
</root>

</xsl:template>
</xsl:stylesheet>

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
computation.


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, www.exslt.org ..

Regards,
Mukul


--- Arvind Bassi <arvind_bassi(_at_)yahoo(_dot_)co(_dot_)uk> 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
http://www.dpawson.co.uk), 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 http://mail.messenger.yahoo.co.uk

 XSL-List info and archive: 
http://www.mulberrytech.com/xsl/xsl-list



__________________________________
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/britneyspears/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list