XSLT and XPath function reference in alphabetical order

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 5, translated from German)

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

fn:boolean

Category:

Type conversion

Origin:

XPath 1.0

Return value:

A Boolean value – true or false.

Call/Arguments:

fn:boolean($inputValue)

$inputValue:

Obligatory. The argument may have any data type. Its value is converted into its effective Boolean value. An argument has to be passed on, but it may also be the empty sequence.

Purpose of use:

In XPath the conversion of a value into a Boolean value is dependent on context and usually done automatically. In certain cases, it may be reasonable to force a conversion by using the fn:boolean() function which is available for this purpose.

The conversion of the input value is made as follows:

  • Sequence
    An empty sequence is evaluated as false; any non-empty sequence whose first item is a node is considered as true.

Exceptions are singleton sequences (corresponding to single atomic values) if they adopt the following values:

  • Number (collective type numeric)
    The number 0 (also +0 and −0) is converted into the Boolean value false; all other number values are true.
  • NaN
    The value NaN is interpreted as false.
  • Character string (data type xs:string or derived from xs:string)
    A string of length zero, which means an »empty« string, becomes false; all other strings become true.
  • URI (data type xs:anyURI or derived from xs:anyURI)
    Returns analogous to the procedure for xs:string character strings the value false if the URI string has the length zero, otherwise true.
  • Untyped atomic type (xs:untypedAtomic)
    Returns analogous to the procedure for xs:string character strings the value false if the value has the length zero, otherwise true.
  • Boolean value (data type xs:boolean or derived from xs:boolean)
    Remains unchanged.

All the other input arguments generate the type error »Invalid argument type« (err:FORG0006).

In the context of XPath 1.0, the boolean() function behaves towards node sets similarly as fn:boolean() towards sequences:

  • Node set (XPath 1.0)
    An empty node set is evaluated with false by the boolean() function. Whereas, each non-empty node set becomes true.
  • Result Tree Fragment (only in XSLT 1.0)
    During the evaluation by boolean(), a result tree fragment is first transformed into its string value and this value is then converted into a Boolean value. The result is true if there are text nodes in the result tree fragment which do not consist exclusively of whitespace, otherwise false.

Example 1 - checking for the presence of an attribute:

<xsl:template match="product">
   <p>serial number present:
   <xsl:text> </xsl:text>
   <xsl:value-of select="fn:boolean(@serialNumber)"/>
   </p>
</xsl:template>

Depending on whether a serialNumber attribute (which represents a node) is present, the example outputs the string »true« or »false«.

Example 2 - checking for alternative child elements:

<xsl:if test="fn:boolean(orderNumber) != fn:boolean(productNumber)">
<p>Order number with product number or neither of the two!</p>
</xsl:if>

The example outputs a message into the result document in case the current node is a child element <orderNumber>, but has no <productNumber> element or vice versa. In this case, the explicit conversion into Boolean values is required since otherwise a comparison of two node sequences would be made – which is not intended in this example.

Function definition:

XPath 1.0:

boolean(value)=> boolean

XPath 2.0:

fn:boolean($arg as item()*) as xs:boolean

   

<< back next >>

 

 

 


Copyright © Galileo Press, Bonn 2008
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "XSLT 2.0 & XPath 2.0" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.


Galileo Press, Rheinwerkallee 4, 53227 Bonn, Germany