Re: [xsl] Getting a distinct list of node names

Subject: Re: [xsl] Getting a distinct list of node names
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 06 Apr 2005 14:51:35 -0400
Paul,

At 02:47 PM 4/6/2005, you wrote:
Hi all,

Back in december 2003 G. Ken Holman posted an excellent solution on this
topic. However I got a question to his solution.

His solution was the following:
  http://www.biglist.com/lists/xsl-list/archives/200312/msg00649.html

Now my question is, what is the generate-id for? When I write
  .=$childnodes[name(.)=name(current())][1]
this works just as fine. Is it advisable not to use comparison of objects,
but rather compare strings or so?

The question is an excellent one; you are thinking along the right lines, but need to take on just a bit more information -- since the actual situation is the reverse of what you're assuming.


The test Ken suggested, using generate-id(), is an effective way in XSLT precisely to "compare objects", whereas testing with the equality operator you are only comparing string values.

This is because of the peculiar way equality is defined to work with node sets. One node set is not only equal to another if all the nodes in the first set are also in the second, and vice-versa, but rather more broadly, if *any* of the nodes in the first set have the same *string value* as any of the nodes in the second set.

What this means in practice is that the equality test is unsafe for establishing node identity. Two node sets that happen to contain the same node always test as equal; they also test as equal if one of the nodes in one set just happens to have the same value as a node in the other.

Since generate-id() is defined to return a unique identifier for any given node irrespective of its value, a string comparison between generated ids is one effective workaround to the problem of establishing node identity.

Another is by counting nodes in combined sets: count(.|$thisnode)=1 iff . and $thisnode are the same node.

Cheers,
Wendell



Yours sincerely,
Paul


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread