RE: [xsl] test expression in predicates - is this one legal???

Subject: RE: [xsl] test expression in predicates - is this one legal???
From: Stuart Brown <sbrown@xxxxxxxxxxx>
Date: Wed, 2 Apr 2003 11:15:36 +0100
Hi Jian,

> <xsl:template match="bar[$foobar='number']">
> number
> </xsl:template>
> is it legal?

This is perfectly legal. The contents of a predicate simply have to be able
to be evaluated to true or false[1], and individual nodes are included or
excluded accordingly. The fact that a predicate "starts at" the context
node, and that most of the time you want to evaluate something in the
context of each specific potential inclusion does not prohibit you from
doing this kind of thing. You can predicate your expression on the value of
a variable/parameter, a node in a completely different part of the document
(using an absolute XPath starting with "/"), on the contents of another
document (using the document() function), or even (using extension
functions) only on a Tuesday.

> is this way considered a normal way of working?
> althou i'd prefer to use <xsl:choose/>

This is just a design decision. Personally, I use xsl:choose when there is a
small difference in handling, and predicated templates when there is a
significant variation. I know one person who believes that, for human
readability, no single XPath should be longer than one line, and therefore
almost always uses xsl:choose, but he's going to have problems the minute
XPath 2 is finalized anyway. People who have a better understanding of how
XSLT processors actually work will probably be able to offer better
recommendations of design practice based upon system expense.

In your instance, of the two templates which could potentially match "bar",
the processor selects that which is most specific. The fact that the
predicate does not actually reference the context node does not stop the
fact that there is a greater degree of specificity in the match value of the
first template.

Hope this helps,


[1] Note that, for instance, "myNode[1]" is shorthand for "myNode[position()
= 1]".

 XSL-List info and archive:

Current Thread