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

Re: How to compare the attributes of two elements to e

Subject: Re: How to compare the attributes of two elements to ensure that they are equal
From: Mukul Gandhi <mukulgw3@xxxxxxxxx>
Date: Wed, 29 Oct 2003 02:05:56 -0800 (PST)
xsl if equal
Hi Arvind,
  The following XSL is a XSLT 1.0 solution to the
problem you had described.. hope, I understood it
correctly ;)

<?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"
extension-element-prefixes="xalan">
<xsl:output method="xml" 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="@*">			      <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-attributes"
select="xalan:nodeset($temp-rtf)/a//att"/>
      <xsl:variable name="c-attributes"
select="xalan:nodeset($temp-rtf)/c//att"/>
	
       <xsl:variable name="result">
	  <xsl:for-each
select="xalan:nodeset($temp-rtf)/a//att">
	     <xsl:for-each
select="xalan:nodeset($temp-rtf)/c//att">
		<xsl:if test="./name =
$a-attributes[position()]/name">					
<xsl:call-template name="calculateResult">						 
<xsl:with-param name="x" select=" 'y' "/>					
</xsl:call-template>
 	      </xsl:if>
	      <xsl:if test="not(./value =
$a-attributes[position()]/value)">
		<xsl:call-template name="calculateResult">
		  <xsl:with-param name="x" select=" 'n' "/>
		</xsl:call-template>
	      </xsl:if>
	</xsl:for-each>
    </xsl:for-each>
    
    <xsl:for-each
select="xalan:nodeset($temp-rtf)/c//att">
	<xsl:for-each
select="xalan:nodeset($temp-rtf)/a//att">
	 <xsl:if test="./name =
$a-attributes[position()]/name">
	  <xsl:call-template name="calculateResult">
	    <xsl:with-param name="x" select=" 'y' "/>
	  </xsl:call-template>
	</xsl:if>
	<xsl:if test="not(./value =
$a-attributes[position()]/value)">
	   <xsl:call-template name="calculateResult">
	      <xsl:with-param name="x" select=" 'n' "/>
	   </xsl:call-template>
        </xsl:if>
     </xsl:for-each>
     </xsl:for-each>
   </xsl:variable>
   
  <xsl:if test="xalan:nodeset($result)/r2[1] =
'notequal' ">
     Attribute sets not equal
   </xsl:if>
   <xsl:if test=" not(xalan:nodeset($result)/r2[1] =
'notequal' )">
        <xsl:if test="xalan:nodeset($result)/r1[1] =
'equal' ">
          Attribute sets equal
        </xsl:if>
   </xsl:if>
</xsl:template>
	
<xsl:template name="calculateResult">
   <xsl:param name="x"/>
   <xsl:if test="$x = 'y' ">
     <r1>equal</r1>
   </xsl:if>
   <xsl:if test="$x = 'n' ">
     <r2>notequal</r2>
   </xsl:if>
</xsl:template>

</xsl:stylesheet>

When the above XSL is applied to the XML --

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <a id1="1" id2="2">one</a>
  <b id1="2">two</b>
  <c id1="1" id2="2">three</c>
  <d id1="4">four</d>
  <e id1="5">five</e>
</root>

, it produces o/p
Attribute sets equal

and, for e.g. when the XSL is applied to the XML --
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <a id1="1" id2="2">one</a>
  <b id1="2">two</b>
  <c id1="1" id2="3">three</c>
  <d id1="4">four</d>
  <e id1="5">five</e>
</root>

, it produces o/p
Attribute sets not equal

You can have any no of attributes(for elements <a> and
<c>), with any names, and attributes can have any
values

You can decide which elements to compare(i.e. their
attribute structure), by modifying 'a' and 'c' within
the XSL..

Hope my answer is useful, and you can adapt it for
your problem..

Regards,
Mukul


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


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.