boolean Function
Converts the argument to a Boolean.
boolean boolean(arg)
Remarks
This function converts arguments to Booleans according to the following rules.
- If the argument is a negative or positive number, it is converted to the Boolean value
true
.false
. - If the argument is a non-empty node-set, it is converted to
true
. An empty node-set is converted tofalse
. - If the argument is a non-empty string, it is converted to
true
. An empty string is converted tofalse
. - If the argument is an object of a type other than the four basic types, it is converted to a Boolean in a way that is dependent on that type. For more information about the basic types of objects, see Using XPath Expressions to Select Nodes.
Example
XML File
None; the XSLT file calls itself.
If you use the sample XML file, books.xml, and change the href
attribute to reference bool.xsl, boolean(//book)
resolves as true
.
XSLT File (bool.xsl)
<?xml version='1.0'?> <?xml-stylesheet type="text/xsl" href="bool.xsl"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template match="/"> <html> <body> <h3>boolean() Function</h3> <ul> <li><b>boolean(0)</b> = <xsl:value-of select="boolean(0)"/> </li> <li><b>boolean(1)</b> = <xsl:value-of select="boolean(1)"/> </li> <li><b>boolean(-100)</b> = <xsl:value-of select="boolean(-100)"/> </li> <li><b>boolean(100)</b> = <xsl:value-of select="boolean(100)"/> </li> <li><b>boolean(NaN)</b> = <xsl:value-of select="boolean(NaN)"/> </li> <li><b>boolean('hello')</b> = <xsl:value-of select="boolean('hello')"/> </li> <li><b>boolean('')</b> = <xsl:value-of select="boolean('')"/> </li> <li><b>boolean(//book)</b> = <xsl:value-of select="boolean(//book)"/> </li> <li><b>boolean(//notfound)</b> = <xsl:value-of select="boolean(//notfound)"/> </li> </ul> </body> </html> </xsl:template> </xsl:stylesheet>
Formatted Output
boolean() Function
- boolean(0) = false
- boolean(1) = true
- boolean(-100) = true
- boolean(100) = true
- boolean(NaN) = false
- boolean('hello') = true
- boolean('') = false
- boolean(//book) = false
- boolean(//notfound) = false
Processor Output
<html> <body> <h3>boolean() Function</h3> <ul> <li><b>boolean(0)</b> = false</li> <li><b>boolean(1)</b> = true</li> <li><b>boolean(-100)</b> = true</li> <li><b>boolean(100)</b> = true</li> <li><b>boolean(NaN)</b> = false</li> <li><b>boolean('hello')</b> = true</li> <li><b>boolean('')</b> = false</li> <li><b>boolean(//book)</b> = true</li> <li><b>boolean(//notfound)</b> = false</li> </ul> </body> </html>
See Also
Data Types in Schemas | XDR Schema Data Types Reference | XML Data Types Reference | NaN Values | Sample XML File (books.xml)