xsl-list
[Top] [All Lists]

Re: [xsl] Is a variable referencing a node

2008-09-27 00:02:31
I said:

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

should have been:

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


-- 
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



On Fri, Sep 26, 2008 at 8:57 PM, Dimitre Novatchev 
<dnovatchev(_at_)gmail(_dot_)com> wrote:
On Fri, Sep 26, 2008 at 6:30 PM, David Frey <dpfrey(_at_)shaw(_dot_)ca> 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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





--
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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--