Subject: Re: [xsl] 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) |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Re: How to compare the attrib, Dimitre Novatchev | Thread | Re: [xsl] How to compare the attrib, Mukul Gandhi |
Re: [xsl] xml and xslt to html attr, Michael Olszynski | Date | RE: [xsl] xml and xslt to html attr, Jarno . Elovirta |
Month |