<html>
<head>
<title>boolean() Function</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div id="Description">
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="main">
<tr>
<td valign="top" class="NAME">boolean() Function</td>
<td valign="top" class="COMPATIBILITY">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
</tr>
<tr>
<td valign="top" colspan="2" class="description">
Converts its argument to a boolean value. </td></tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Inputs</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>An object. The object is converted to a boolean value. This conversion is described in the following subsection. </p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Output</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>The boolean value corresponding to the input object. Objects are converted to boolean values as follows:</p>
<ul>
        <dd>A number is <span class="LITERAL">true</span> if and only if it is not zero, negative zero, or <span class="LITERAL">NaN</span> (not a number).</p>
        </li>
        <dd>A node-set is <span class="LITERAL">true</span> if and only if it is not empty.</p>
        </li>
        <dd>A string is <span class="LITERAL">true</span> if and only if its length is greater than zero.</p>
        </li>
        <dd>All other datatypes are converted in a way specific to those datatypes.</p>
        </li>
      </ul>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Defined in</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>XPath section 4.3, Boolean Functions.</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Example</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>The following example demonstrates the results of invoking the <span class="LITERAL">boolean()</span> function against a variety of argument types. Here's our XML document:</p>
<span class="PROGRAMLISTING"><pre>
&lt;?xml version="1.0"?&gt;
&lt;test&gt;
&lt;p&gt;This is a test XML document used by several 
of our sample stylesheets.&lt;/p&gt;
&lt;question&gt;
&lt;text&gt;When completed, the Eiffel Tower was the 
tallest building in the world.&lt;/text&gt;
&lt;true&gt;Yes!  The Eiffel Tower was the world's 
tallest building until 1932, when
New York's Empire State Building opened. &lt;/true&gt;
&lt;false&gt;No, the Eiffel Tower was the world's tallest 
building for over 30 years.&lt;/false&gt;
&lt;/question&gt;
&lt;/test&gt;</pre></span>
<p>We'll process this document with the following stylesheet:</p>
<span class="PROGRAMLISTING"><pre>
&lt;?xml version="1.0"?&gt;
&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  &lt;xsl:output method="text"/&gt;

  &lt;xsl:variable name="newline"&gt;
&lt;xsl:text&gt;
&lt;/xsl:text&gt;
  &lt;/xsl:variable&gt;

  &lt;xsl:template match="/"&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;Tests of the boolean() function:&lt;/xsl:text&gt;

    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:choose&gt;
      &lt;xsl:when test="boolean(true())"&gt;
        &lt;xsl:text&gt;   "boolean(true())"   returned true!&lt;/xsl:text&gt;
      &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
        &lt;xsl:text&gt;   "boolean(true())"   returned false!&lt;/xsl:text&gt;
      &lt;/xsl:otherwise&gt;
    &lt;/xsl:choose&gt;

    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:choose&gt;
      &lt;xsl:when test="boolean(true)"&gt;
<!--<?troff .Nd 10?>-->
        &lt;xsl:text&gt;   "boolean(true)"     returned true!&lt;/xsl:text&gt;
      &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
        &lt;xsl:text&gt;   "boolean(true)"     returned false!&lt;/xsl:text&gt;
      &lt;/xsl:otherwise&gt;
    &lt;/xsl:choose&gt;

    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:choose&gt;
      &lt;xsl:when test="boolean('false')"&gt;
        &lt;xsl:text&gt;   "boolean('false')"  returned true!&lt;/xsl:text&gt;
      &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
        &lt;xsl:text&gt;   "boolean('false')"  returned false!&lt;/xsl:text&gt;
      &lt;/xsl:otherwise&gt;
    &lt;/xsl:choose&gt;

    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:choose&gt;
      &lt;xsl:when test="boolean('7')"&gt;
        &lt;xsl:text&gt;   "boolean('7')"      returned true!&lt;/xsl:text&gt;
      &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
        &lt;xsl:text&gt;   "boolean('7')"      returned false!&lt;/xsl:text&gt;
      &lt;/xsl:otherwise&gt;
    &lt;/xsl:choose&gt;

    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:choose&gt;
      &lt;xsl:when test="boolean(/true)"&gt;
        &lt;xsl:text&gt;   "boolean(/true)"    returned true!&lt;/xsl:text&gt;
      &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
        &lt;xsl:text&gt;   "boolean(/true)"    returned false!&lt;/xsl:text&gt;
      &lt;/xsl:otherwise&gt;
    &lt;/xsl:choose&gt;

    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:choose&gt;
      &lt;xsl:when test="boolean(//true)"&gt;
        &lt;xsl:text&gt;   "boolean(//true)"   returned true!&lt;/xsl:text&gt;
      &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
        &lt;xsl:text&gt;   "boolean(//true)"   returned false!&lt;/xsl:text&gt;
      &lt;/xsl:otherwise&gt;
    &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;</pre></span>
<p>Here are the results:</p>
<span class="PROGRAMLISTING"><pre>

Tests of the boolean() function:

   "boolean(true())"   returned true!
   "boolean(true)"     returned false!
   "boolean('false')"  returned true!
   "boolean('7')"      returned true!
   "boolean(/true)"    returned false!
   "boolean(//true)"   returned true!
</pre></span>
<p>See <link linkend="xslt-CHP-4-SECT-2.1.2">Section 4.2.1.2</link> in <link linkend="xslt-CHP-4">Chapter 4</link> for more examples and information.</p>
</td>
</tr>
</table>
</div>
</body>
</html>