Re: [xsl] Hello and quick question

Subject: Re: [xsl] Hello and quick question
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 07 Jan 2003 14:01:05 -0500
What's worse, the expression:

/ROOT/Client/Contacts/Contact[isReferrer='0'][1]/ContactFirstName

will give results that are counterintuitive, since the [1] predicate operates not on the expression as a whole, but only on that particular step of that expression,

"Contact[isReferrer='0'][1]"

which (as all XSLT users certainly know!) is short for

child::Contact[isReferrer='0'][position()=1]

which means you'll get, not the first such Contact node in the document, but every Contact node (that meets the isReferrer criterion) *that is the first such child of its parent*.

To get around this, you can say

(/ROOT/Client/Contacts/Contact[isReferrer='0'])[1]/ContactFirstName

(Grouping the entire expression indicates the predicate [1] applies to the expression as a whole.)

But I actually rather like Corey's solution

string(/ROOT/Client/Contacts/Contact[isReferrer='0']/ContactFirstName)

...although were I to use it in a stylesheet, I'd certainly explain it in a comment -- as something non-obvious that might be expected to break if, say, the rules of how node-sets are converted into strings should change....

Cheers,
Wendell

At 11:36 PM 1/6/2003, Joerg's wrote:
Maybe you can better use some possible optimizations of the proceesor by adding [1] into the expression:

/ROOT/Client/Contacts/Contact[isReferrer='0'][1]/ContactFirstName

Furthermore you sometimes need a node set (even it's a single node) and not a string, e.g. <xsl:apply-templates select="/ROOT/Client/Contacts/Contact[isReferrer='0'][1]/ContactFirstName"/>, that won't work with string().

Simon has a little error in his expression. He selects the first Contact element, independent of [isReferrer='0'] or not. If this first Contact element does not have a child 'isReferrer' with text value '0', his node set would be empty.


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


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread