Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > comparing nodes in xslt

Reply
Thread Tools

comparing nodes in xslt

 
 
inquirydog
Guest
Posts: n/a
 
      09-28-2004
Hi-

Does anyone know a way to compare whether two nodes contain
the same information in xslt (the name, attributes, and all content
recursivly should be the same. I am interested in the case where node
ordering matters, and also the case where it doesn't, but perhaps that
is an advanced topic). Ideally the method should be available to
xpath expressions, so I think that creating new templates which
compare nodes will not work (well, it will if you store the results of
the apply-template in a variable and then use that in an expression,
but this seems way too complex at times, especially since variables
are fixed after they are evaluated).

thanks
-I
 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      09-29-2004
Hi,

I'm not quite sure this reply will be of any use to you.
This solution only works with nodes with the same ordering.
As far as I know, there are not (yet) Xpath expressions to deal with
node comparison (or operations).

to compare e.g. node[1] and node[2], use :

<xsl:variable name="identical">
<xsl:call-template name="check_identical">
<xsl:with-param name="comp1"><xsl:copy-of
select="//node[1]"/></xsl:with-param>
<xsl:with-param name="comp2"><xsl:copy-of
select="//node[2]"/></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:if test="$identical='true'">nodes completely are
identical</xsl:if>
<xsl:if test="$identical='false'">nodes differ in at least one
aspect</xsl:if>

These 2 templates should be included/inserted in the stylesheet:

<xsl:template name="check_identical">
<xslaram name="comp1"/>
<xslaram name="comp2"/>
<xsl:variable name="string1">
<xsl:call-template name="stringify">
<xsl:with-param name="node"><xsl:copy-of
select="$comp1"/></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="string2">
<xsl:call-template name="stringify">
<xsl:with-param name="node"><xsl:copy-of
select="$comp2"/></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$string1=$string2"/>
</xsl:template>

The following template 'stringifies' a given node i.e. convert it to
the ascii equivalent like one would see when opening an xml file with
a text editor.

<xsl:template name="stringify">
<xslaram name="node"/>
<xsl:for-each select="$node/*/*|$node/*/text()">
<xsl:choose>
<xsl:when test="boolean(local-name())">
&lt;<xsl:value-of select="local-name()"/>
<xsl:variable name="pos" select="position()"/>
<xsl:for-each select="@*">
<xsl:text> </xsl:text><xsl:value-of
select="local-name()"/>="<xsl:value-of select="."/>"
</xsl:for-each>
<xsl:call-template name="stringify">
<xsl:with-param name="node"><xsl:copy-of
select="."/></xsl:with-param>
</xsl:call-template>
&lt;/<xsl:value-of select="local-name()"/>&gt;
</xsl:when>
<xsltherwise><xsl:value-of
select="normalize-space(.)"/></xsltherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>

Joris Gillis
 
Reply With Quote
 
 
 
 
inquirydog
Guest
Posts: n/a
 
      10-01-2004
Hi-

Thanks for the reply. I looked into it a bit and found that
in XSLT 2 you can define xpath functions using xslt elements. This
certainly makes a solution like this more reasonable. It does still
leave certain problems like counting repeat nodes in a list.... For
instance, if you wanted to eliminate doubles in a list like this

<list>
<element name="1" />
<element name="2" />
<element name="1" />
<element name="1" />
<element name="1" />
<element name="2" />
</list>

converting to

<list>

<element name"1" count="4" />
<element name="2" count="2" />

</list>

you need to group by element type and then count the number of
elements. Except for very constrained cases this seems to me to be
impossible to do in xslt (yes I know about the muenchian method- it
only works in a constrained case). To do this grouping you need to
first be able to check for node equality (which I think you can do in
xslt 2) and then do the grouping based on the equality (which is
better supported in xslt 2 but still can't do what I describe in this
email).

thanks
-I


> Hi,
>
> I'm not quite sure this reply will be of any use to you.
> This solution only works with nodes with the same ordering.
> As far as I know, there are not (yet) Xpath expressions to deal with
> node comparison (or operations).
>
> to compare e.g. node[1] and node[2], use :
>
> <xsl:variable name="identical">
> <xsl:call-template name="check_identical">
> <xsl:with-param name="comp1"><xsl:copy-of
> select="//node[1]"/></xsl:with-param>
> <xsl:with-param name="comp2"><xsl:copy-of
> select="//node[2]"/></xsl:with-param>
> </xsl:call-template>
> </xsl:variable>
> <xsl:if test="$identical='true'">nodes completely are
> identical</xsl:if>
> <xsl:if test="$identical='false'">nodes differ in at least one
> aspect</xsl:if>
>
> These 2 templates should be included/inserted in the stylesheet:
>
> <xsl:template name="check_identical">
> <xslaram name="comp1"/>
> <xslaram name="comp2"/>
> <xsl:variable name="string1">
> <xsl:call-template name="stringify">
> <xsl:with-param name="node"><xsl:copy-of
> select="$comp1"/></xsl:with-param>
> </xsl:call-template>
> </xsl:variable>
> <xsl:variable name="string2">
> <xsl:call-template name="stringify">
> <xsl:with-param name="node"><xsl:copy-of
> select="$comp2"/></xsl:with-param>
> </xsl:call-template>
> </xsl:variable>
> <xsl:value-of select="$string1=$string2"/>
> </xsl:template>
>
> The following template 'stringifies' a given node i.e. convert it to
> the ascii equivalent like one would see when opening an xml file with
> a text editor.
>
> <xsl:template name="stringify">
> <xslaram name="node"/>
> <xsl:for-each select="$node/*/*|$node/*/text()">
> <xsl:choose>
> <xsl:when test="boolean(local-name())">
> &lt;<xsl:value-of select="local-name()"/>
> <xsl:variable name="pos" select="position()"/>
> <xsl:for-each select="@*">
> <xsl:text> </xsl:text><xsl:value-of
> select="local-name()"/>="<xsl:value-of select="."/>"
> </xsl:for-each>
> <xsl:call-template name="stringify">
> <xsl:with-param name="node"><xsl:copy-of
> select="."/></xsl:with-param>
> </xsl:call-template>
> &lt;/<xsl:value-of select="local-name()"/>&gt;
> </xsl:when>
> <xsltherwise><xsl:value-of
> select="normalize-space(.)"/></xsltherwise>
> </xsl:choose>
> </xsl:for-each>
> </xsl:template>
>
> Joris Gillis

 
Reply With Quote
 
David Carlisle
Guest
Posts: n/a
 
      10-04-2004

http://www.velocityreviews.com/forums/(E-Mail Removed) (inquirydog) writes:

> Hi-
>
> Thanks for the reply. I looked into it a bit and found that
> in XSLT 2 you can define xpath functions using xslt elements. This
> certainly makes a solution like this more reasonable. It does still
> leave certain problems like counting repeat nodes in a list.... For
> instance, if you wanted to eliminate doubles in a list like this
>
> <list>
> <element name="1" />
> <element name="2" />
> <element name="1" />
> <element name="1" />
> <element name="1" />
> <element name="2" />
> </list>
>
> converting to
>
> <list>
>
> <element name"1" count="4" />
> <element name="2" count="2" />
>
> </list>
>
> you need to group by element type and then count the number of
> elements. Except for very constrained cases this seems to me to be
> impossible to do in xslt (yes I know about the muenchian method- it
> only works in a constrained case). To do this grouping you need to
> first be able to check for node equality (which I think you can do in
> xslt 2) and then do the grouping based on the equality (which is
> better supported in xslt 2 but still can't do what I describe in this
> email).
>


What do you mean by

> but still can't do what I describe in this email.


?

Your grouping example seems quite simple to do in either XSLT1 or XSLT2,
it might loook a bit simpler in 2 using the explict xsl:for-each-group
mechanism, but it doesn't take much code in either case.

For example in 1.0 it would be:

<xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:key name="x" match="element" use="@name"/>

<xsl:template match="list">
<list>
<xsl:for-each select="element[generate-id()=generate-id(key('x',@name))]">
<element name="{@name}" count="{count(key('x',@name))}"/>
</xsl:for-each>
</list>
</xsl:template>

</xsl:stylesheet>



David
 
Reply With Quote
 
inquirydog
Guest
Posts: n/a
 
      10-07-2004
Hi-

The method you describe is the Muenchian Method that I
mentioned in my posting.... And it only works for very simple cases,
where 1). you know ahead of time where the list to be counted is, and
2). where the list items are trivial content (all of the same type).
For instance just change one of the elements by adding content to it

<element name="1"> => <element name="1"><content /></element>

the example counts this as the same type of element as the ones
without content.

I repeat what I stated before- XSLT 1.0 and 2.0 are (I am pretty sure)
not capable of counting repeat nodes in a list in the general case,
where the list can appear anywhere in the node tree and where the
elements in the list are not of some predetermined type (they can
contain arbitrary content). This is a limitation of xslt that I
believe should be fixed in future revisions.

thanks
-I


> Your grouping example seems quite simple to do in either XSLT1 or XSLT2,
> it might loook a bit simpler in 2 using the explict xsl:for-each-group
> mechanism, but it doesn't take much code in either case.
>
> For example in 1.0 it would be:
>
> <xsl:stylesheet xmlnssl="http://www.w3.org/1999/XSL/Transform" version="1.0">
>
> <xsl:key name="x" match="element" use="@name"/>
>
> <xsl:template match="list">
> <list>
> <xsl:for-each select="element[generate-id()=generate-id(key('x',@name))]">
> <element name="{@name}" count="{count(key('x',@name))}"/>
> </xsl:for-each>
> </list>
> </xsl:template>
>
> </xsl:stylesheet>

 
Reply With Quote
 
David Carlisle
Guest
Posts: n/a
 
      10-08-2004


You say

<element name="1"> => <element name="1"><content /></element>

the example counts this as the same type of element as the ones
without content.

well yes of course because you (and so I) are only grouping on the name
attribute. If you want to consider something else as being a
distinguishing factor then just include it in the test eg
use
concat(@name,':',count(*))
would distinguish nodes based on their name attribute and the number of
children. I suspect that you want a test that is more like deep-equal
(which is a predefined function in xpath2) so perhaps your request
> This is a limitation of xslt that I
> believe should be fixed in future revisions.


has been granted in Xpath2. If that is the case you probably can't express that as
a single Xpath in Xpath 1 (without using an extension function,) but you
can certainly express it in xslt1 so you can do the grouping, you
probably just can't use the optimisation of using a key as the key index
needs to be expressed as a single xpath. But keys are only an
optimisation feature anything expressable using keys is expressable
without them (just may take longer to run)

of course any xpath extension that allows xpath functions to be defined
using xslt would allow this kind of key to be used even in an xslt1
context (saxon and the exslt project have such function definition
possibilitiesfor xslt1, and they are a standard part of xslt2 draft)

David
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT: Selecting nodes by comparing data AFTER conversion Ryan Adler XML 2 01-31-2006 09:53 AM
Text nodes and element nodes query asd Java 3 05-23-2005 10:01 AM
Looking A Nodes From Within Nodes Johnny Ooi XML 10 11-14-2004 06:55 PM
selecting nodes between other nodes Timo Nentwig XML 1 06-17-2004 04:54 AM
Reality check: Is it sensible to link XML nodes to other XML nodes in the same file? gavnosis XML 0 08-02-2003 08:22 AM



Advertisments