empty variable false?
Added by Anonymous over 19 years ago
Legacy ID: #3239756 Legacy Poster: daiei27 (daiei27)
Wasn't able to find it in the spec even though I think I've seen a list of true/false values somewhere. So shouldn't an empty variable be false? Here's the variable code in short: <variable name=x> <for-each test=...> <sequence ...> </for-each> <variable> Later I want to simply test if any items were added to the variable. I have an if statement that starts like this: <xsl:if test="$x"> I would think that would be false if no sequences were added, but it always returns true using Saxon 8.xb. It seems to work the way I like in Altova's XMLSpy.
Replies (4)
Please register to reply
RE: empty variable false? - Added by Anonymous over 19 years ago
Legacy ID: #3239850 Legacy Poster: DD (ddevienne)
You might want to try to type your variable. I've seen weird things (i.e. that I didn't expect) with vars until I added as="node()*" to its declaration. Something about atomization going on unless the var is declared to be a var of nodes maybe?! --DD
RE: empty variable false? - Added by Anonymous over 19 years ago
Legacy ID: #3239893 Legacy Poster: Jan Van den Bussche (vdbuss)
Since you put the sequence constructor as content in the variable statement, a temporary tree is going to be constructed, and it will always have at least a document node, even if the sequence itself is empty. In effect the value of x will be the singleton sequence consisting of that document node. So, x is nonempty and therefore is converted to true. (Curious whether Michael will approve this ;-) --Jan
RE: empty variable false? - Added by Anonymous over 19 years ago
Legacy ID: #3240075 Legacy Poster: daiei27 (daiei27)
That's funny. I just wrote up a bug on XMLSpy because they don't create a document node inside a variable. Guess I can't fault them for it since I didn't even realize what it entailed. Well Jan's explanation seems to make sense, but I would think the document node shouldn't be created until content is created. I guess that might be inconvenient since the XSLT processor might have to make that decision at compile time. I suppose I could just type the variable as DD suggested, but it would be so much nicer my way. :D Thanks.
RE: empty variable false? - Added by Anonymous over 19 years ago
Legacy ID: #3240185 Legacy Poster: Michael Kay (mhkay)
Jan's explanation is correct. If an xsl:variable element has no "as" attribute, then the value of the variable will always be a document node, and the effective boolean value of a sequence containing a document node is true. It has to be this way for compatibility with XSLT 1.0. Michael Kay
Please register to reply