[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Is a variable referencing a node

Subject: Re: Is a variable referencing a node
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Fri, 26 Sep 2008 20:57:04 -0700
Re:  Is a variable referencing a node
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2011 All Rights Reserved.