value of an uninitialised variable

A

Andy Fish

consider the following fragment of xsl:

<xsl:variable name="v1"><xsl:if test="false()"/></xsl:variable>
<xsl:variable name="v2"></xsl:variable>
<xsl:value-of select="boolean($v1)"/>
<xsl:value-of select="boolean($v2)"/>

it returns true for v1 but false for v2. (I'm using microsoft .net 1.1)

how so? presumably v1 is a non-empty node set but I don't see how it could
have any nodes in it.

Andy
 
D

David Carlisle

Andy Fish said:
consider the following fragment of xsl:

<xsl:variable name="v1"><xsl:if test="false()"/></xsl:variable>
<xsl:variable name="v2"></xsl:variable>
<xsl:value-of select="boolean($v1)"/>
<xsl:value-of select="boolean($v2)"/>

it returns true for v1 but false for v2. (I'm using microsoft .net 1.1)

how so? presumably v1 is a non-empty node set but I don't see how it could
have any nodes in it.

Andy


neither $v1 nor $v2 hold a node set.

The variable binding for v1 has no select attribute but does have
content, so it generates a reult tree fragment. this rtf will act in a
boolean context as a node set with a document node with child a text
node (with value the empty string). As this node set is non empty it
evaluates to true() in a boolean context.

The variable binding for v1 has no select attribute and no content. This
is special cased in teh XSLT spec and doesn't produce a result tree
fragment, it produces an empty string. empty strings evaluate to false()
in a boolean context.

If you want both tests to act the same way replace boolean() (which is
redundant, actually) by string(),

David
 
R

Richard Tobin

Andy Fish said:
<xsl:variable name="v1"><xsl:if test="false()"/></xsl:variable>

The value of v1 is a result tree fragment. This is like a root node with
(in this case) no children. As a node-set, it is non-empty.
<xsl:variable name="v2"></xsl:variable>

The value of v2 is an empty string (because it has no content and no
select attribute: see the spec).

-- Richard
 
R

Richard Tobin

David Carlisle said:
The variable binding for v1 has no select attribute but does have
content, so it generates a reult tree fragment. this rtf will act in a
boolean context as a node set with a document node with child a text
node (with value the empty string).

Why an empty text child? Why not no child?

-- Richard
 
A

Andy Fish

David Carlisle said:
neither $v1 nor $v2 hold a node set.

The variable binding for v1 has no select attribute but does have
content, so it generates a reult tree fragment. this rtf will act in a
boolean context as a node set with a document node with child a text
node (with value the empty string). As this node set is non empty it
evaluates to true() in a boolean context.

I don't really see why v1 should have any nodes in the result tree fragment.

presumably if I had written

<xsl:variable name="v1"><foo /><bar /></xsl:variable>

then it would be a RTF with 2 nodes?

If that's just the way it is then I guess I'll just have to accept it, but I
don't see any logical reason for it.
 
D

David Carlisle

presumably if I had written

<xsl:variable name="v1"><foo /><bar /></xsl:variable>

then it would be a RTF with 2 nodes?


No _any_ rtf variable corresponds to a node set of exactly one node a
root node corresponding to the top of the virtual document created.

<xsl:variable name="x">
... anything (but not nothing) ...
</xsl:variable>

_always_ produces something for which
<xsl:value-of select="xx:node-set($x)"/>
is 1, so always produces something that acts as true in a boolean
context.

If that's just the way it is then I guess I'll just have to accept it, but I
don't see any logical reason for it.

In Xpath 1 every element has to have a parent so if you go

<xsl:variable name="v1"><foo /><bar /></xsl:variable>

you get a result tree fragment corresponding to a node set with exactly
one node, a root node with two element children.

Xpath2 allows parentless element nodes to exist and XSLT draft
optionally allows you to create these, so in xslt2 you can have
a variable containing a sequence of two parentless nodes, if you wish.
(but being parentless, they are not siblings, so Xpath axes don't always
work as you might expect for these beasts).

David
 
D

David Carlisle

I wrote

<xsl:variable name="x">
... anything (but not nothing) ...
</xsl:variable>

_always_ produces something for which
<xsl:value-of select="xx:node-set($x)"/>
is 1, so always produces something that acts as true in a boolean
context.


that should have been

<xsl:value-of select="count(xx:node-set($x))"/>
^^^^^

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top