[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Is a variable referencing a node


Subject: Re: [xsl] Is a variable referencing a node
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Fri, 26 Sep 2008 20:57:04 -0700

On Fri, Sep 26, 2008 at 6:30 PM, David Frey <dpfrey@xxxxxxx> wrote:
> I have an XSLT 1 question.
>
> Say I have:
>
> <xsl:variable name="foo">
>  <xsl:choose>
>    <xsl:when test="$aVar='someFlag'">
>      <xsl:value-of select="/path/to/some/element"/>
>    </xsl:when>
>    <xsl:otherwise>
>      <xsl:value-of select="/path/to/another/element"/>
>    </xsl:otherwise>
> </xsl:variable>
> <xsl:choose>
>  <xsl:when test="$foo">
>    <xsl:value-of select="$foo"/>
>  </xsl:when>
>  <xsl:otherwise>--</xsl:otherwise>
> </xsl:choose>
>
>
>
> Currently, I never get "--" as my output even if $aVar is set to
> 'someFlag' and the element at "/path/to/some/element" does not exist.
>
> Can someone explain where I went wrong?

The variable  $foo is of type RTF (Result Tree Fragment).

The XSLT 1.0 spec says:

"A result tree fragment represents a fragment of the result tree. A
result tree fragment is treated equivalently to a node-set that
contains just a single root node" ...

and a few lines later:

"When a permitted operation is performed on a result tree fragment, it
is performed exactly as it would be on the equivalent node-set."

http://www.w3.org/TR/xslt#section-Result-Tree-Fragments

The <xsl:when/> is evaluated as per spec:

"When an xsl:choose element is processed, each of the xsl:when
elements is tested in turn, by evaluating the expression and
converting the resulting object to a boolean as if by a call to the
boolean function."

http://www.w3.org/TR/xslt#section-Conditional-Processing-with-xsl:choose

this means that the value of

  boolean($foo)

is determined and it is the same as when boolean() is applied on the
equivalent node-set of the RTF $foo.

Because the equivalent node-set of an RTF always consists of just one
node (a root node), and the rules for boolean() say:

"a node-set is true if and only if it is non-empty"

http://www.w3.org/TR/xpath#section-Boolean-Functions


this means that boolean($foo) is always true().

This is why, the first <xsl:when> in the code in question is always chosen:

> <xsl:choose>
>  <xsl:when test="$foo">
>    <xsl:value-of select="$foo"/>
>  </xsl:when>
>  <xsl:otherwise>--</xsl:otherwise>
> </xsl:choose>

and the <xsl:otherwise> above is never chosen.

BTW, the definition of the boolean() function in the XSLT (1.0)
Programmer's Reference that I have is not correct (on page 427),
saying for boolean(result-tree-fragment) that:

""The result tree fragment is first converted to a string, and the
string is then converted to a Boolean.
The resulting Boolean is true if the result tree fragment contains any
non-empty text nodes, and is false otherwise"

According to this definition, boolean($foo) would be false, although
its equivalent node-set is not empty.

Saxon 6.5.3 certainly behaves correctly and evaluates boolean($foo) to
true even when the equivalent node-set contains no nodes.



Cheers,
Dimitre Novatchev.






>
> Thanks,
> David
>
>



-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play


Current Thread
Keywords