RE: [xsl] Using XSLT 1.0 , I have tried using the Meunchian methodsolutions on

Subject: RE: [xsl] Using XSLT 1.0 , I have tried using the Meunchian methodsolutions on
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 20 Dec 2006 11:59:24 -0500
At 06:57 PM 12/19/2006, you wrote:
I thought he was already using nested loops?  The outer looped through
the nodes selected by the key, then the inner iterated through all nodes
selected by the key using the FilingType of the context node in the
outer loop.  Since he wished to display AgencyName once in the inner
loop, using position() should be appropriate there.  I could have
misread the original post, of course.

No, Brian, in the context, you're correct. I was just cautioning that the same thing might not work in just any context.


position() is tricky and often the source of various mysterious bugs, because it usually works (as in this context), but then in another context it doesn't.

Specifically, when you say for-each select="key('a','string')"> the current node list includes the nodes returned by the key with the value 'b' in document order, and only those nodes, so it's safe to use position(). But if, for example, you say apply-templates and select some "invisible" text nodes, or if you say key('a',path) and there is more than one node in path, you may get more nodes than you expect, and position() may throw you off.

I mention this position() peskiness almost every time it comes up on the list, not because the solutions offered won't work (they usually will), but because they often run a risk of misleading the inexperienced, and hence of causing code to break when the "same" technique is used elsewhere. It's natural to think that position() refers to the position of a node in the source tree relative to others. But it doesn't. (And keep in mind that many people are learning XSLT by reading solutions to others' problems on the list.)

And because it's obscure, problems related to use of position() are almost impossible to debug if you're not already in the know.

Cheers,
Wendell



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