tags:

views:

42

answers:

2

I need a test for a variable which would evaluate to true in two cases:

  • There is a string inside which contains any non white space characters
  • There is any node (which can be possibly empty)

and the variable is filled with apply-template call result.

I tried

test="normalize-space($var)"

but this doesn't cover the empty tag possibility. I also tried simply this:

test="$var"

but this evaluate to true even for white space only strings.

By the way "$var/*" produces an error "Expression ...something I don't remember... node-set" which is I think because of apply-template variable instantiation.

Is there any (which means even multi level decision) solution for this?

EDIT: I forgot to say that it's for XSLT 1.0 and preferably without any exslt extensions or similar.

A: 

Hope this helps:

"string($variable)" will test true if the string has any characters in it, false if it equals ''.

http://www.dpawson.co.uk/xsl/sect2/N3328.html#d4474e64

Contains no child nodes: not(node())

Contains no text content: not(string(.))

Contains no text other than whitespace: not(normalize-space(.))

Contains nothing except comments: not(node()[not(self::comment())])

James Campbell
Those are basic tools, but I need to combine them to solve my problem. Could you please provide the exact test? Maybe I'm just dumb, but maybe you find out that it's not that easy. Mainly because you can't use "$var/*" as I have written (you would need to apply some sort of node-set() before). By the way, how do you apply node() on a variable?
calavera.info
I added that for looking for empty strings in a node etc.. just additional, Sorry don't have time to write the code atm, will take a look in the am. I did provide how to find if variable is empty. If you apply the above you have your answer.
James Campbell
string() for empty node returns empty string,node() can't be used because variable is NOT a node set, although it contains element.normalize-space() for empty node returns empty string=> I don't know, how to apply above...
calavera.info
+1  A: 

If you want to check whether a result tree fragment contains any child nodes then you need exsl:node-set e.g.

<xsl:if test="exsl:node-set($variable)/node()"
   xmlns:exsl="http://exslt.org/common"&gt;

Without that extension function (or a similar one your particular XSLT 1.0 processor offers) you can't perform that check.

Martin Honnen
I hoped for exslt free solution but at least thanks for confirming it's not possible.
calavera.info