Re: [xsl] When test expression

Subject: Re: [xsl] When test expression
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Wed, 28 Mar 2001 07:46:29 +0100
Hi Paul,

> I'd like to have the test for A/B only succeed when the
> value of the B node is not "" (producing <SPAN>Orange</SPAN>
> in the example).
>
> I've tried
>
>    <xsl:when test="A/B[not(.)]"/>

Just to explain why this doesn't work: the . means 'the context node'.
There's always a context node, so converting a node set holding the
context node to a boolean always results in true. That means the
predicate is always false, which means that the path never generates
any nodes (it gives an empty node set). Converting an empty node set
to a boolean always results in false.

> but this does not work. Any suggestions?

Given that A/B only results in one node, you want:

  string(A/B)

This converts the value of the B element to a string.  If there's no
textual content in the B element, then it gives the empty string.
When the empty string is converted to a boolean (because it's in a
test) then it's coverted to false, and the test fails.  If there is
textual content, it gives that textual content.  This non-empty string
is converted to a boolean and results in true, and the test succeeds.

You might want to use:

  normalize-space(A/B)

instead, as this will fail for B elements that hold only whitespace,
e.g.:

  <B>   </B>

One other thing.  In your XSLT sample you use:

  <xsl:node-name />

which isn't an XSLT instruction.  I think you mean:

  <xsl:value-of select="name()" />

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



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


Current Thread