xsl-list
[Top] [All Lists]

Re: [xsl] Is a variable referencing a node

2008-09-29 11:29:56
On 9/27/2008, "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.



Thanks for the detailed explanation.  It sounds like the approach I was
using will not work.  Is there another way that I can write this
template?

Basically, I want a template that will output the text of a node for an
arbitrary XPath if that node exists.  If the node doesn't exist, I want
it to produce "--".


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