value of an uninitialised variable

Discussion in 'XML' started by Andy Fish, Jan 10, 2005.

  1. Andy Fish

    Andy Fish Guest

    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
    Andy Fish, Jan 10, 2005
    #1
    1. Advertising

  2. "Andy Fish" <> writes:

    > 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
    David Carlisle, Jan 10, 2005
    #2
    1. Advertising

  3. In article <H2uEd.7535$>,
    Andy Fish <> wrote:

    ><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
    Richard Tobin, Jan 10, 2005
    #3
  4. In article <>,
    David Carlisle <> wrote:

    >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
    Richard Tobin, Jan 10, 2005
    #4

  5. > Why an empty text child? Why not no child?


    because I was wrong. I would have followed up, but by then you'd already
    posted the right answer.

    David
    David Carlisle, Jan 10, 2005
    #5
  6. Andy Fish

    Andy Fish Guest

    "David Carlisle" <> wrote in message
    news:...
    > "Andy Fish" <> writes:
    >
    >> 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.
    >


    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.

    > 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
    Andy Fish, Jan 10, 2005
    #6
  7. 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
    David Carlisle, Jan 10, 2005
    #7
  8. 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
    David Carlisle, Jan 10, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gene Wirchenko
    Replies:
    22
    Views:
    715
    Jeffrey Schwab
    Dec 19, 2003
  2. Replies:
    2
    Views:
    270
    Rolf Magnus
    Feb 4, 2007
  3. Replies:
    2
    Views:
    284
    John Ratliff
    Feb 4, 2007
  4. Cognition Peon

    use of uninitialised value

    Cognition Peon, Apr 15, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    142
    Ala Qumsieh
    Apr 15, 2004
  5. Len
    Replies:
    3
    Views:
    186
Loading...

Share This Page