XSLT and XPath function reference in alphabetical order
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 5, translated from German)
The fn:not() function outputs the Boolean negation of its argument (converted to a Boolean value). If it is true, the return value is false and vice versa.
The input value of the function must be a Boolean value. If it is not a Boolean value, the input is converted to its effective Boolean value according to the fn:boolean() function. If the empty sequence is passed on to the function, it returns false.
Purpose of use:
The fn:not() function is often used in the context of the formulation of conditions for xsl:if or the like in order to express the Boolean negation of a value passed on.
In advance, the argument is converted to a Boolean value, if required. In other words, the function behaves analogously to the fn:boolean() function, but with the same input it returns the opposite value to fn:boolean().
In this connection should be mentioned that at first sight the apparently equivalent expressions
fn:not($operand1 = $operand2)
$operand1 != $operand2
are interpreted differently. The fn:not() function negates the entire expression, which means it returns false, if the two operands of the inner expression are equivalent, and true, if they are unequal. This also applies for the second expression, but only in case the two compared operands actually do exist.
In the test for equality with = as well as in the test for inequality with !=, also the meaning »if the operands exist« is implicitly contained: If one of the two does not exist, the result is always false. So, the second expression is false in case of inequality of both operands as well as in case of nonexistence of one of the two.
The fn:not() function encapsulates the inner expression and therefore, it behaves differently. The expression in the example above returns true if $operand1 is unequal to $operand2, but also if one of the two operands does not exist. This is due to the fact that the inner expression – as described above – is false in this case.
Example 1 – test for nonexistence of child nodes:
<xsl:if test="fn:not(node())"> <!-- content constructor --> </xsl:if>
This test using the node() node test as an aid returns true if the current node has no child nodes. In this case the template body in the inside is executed.
Example 2 – exclusion for xsl:for-each loop:
<xsl:for-each select="*[fn:not(self::example)]"> <!-- content constructor --> </xsl:for-each>
At first, the xsl:for-each statement selects all child elements * of the current node, but excludes the <example> elements via a [fn:not(self::example)] predicate.
Example 3 – testing whether an attribute is missing or empty:
<xsl:if test="fn:not(fn:string(@price))"> <xsl:text>No price indication available!</xsl:text> </xsl:if>
In this example, a test is made whether a price attribute of the current node is empty or does not exist at all. It uses the fn:string() function as an aid in order to convert the attribute value to a string. This is necessary because otherwise only the nonexistence of the attribute could be detected, but not whether it contains an empty string. When negated, the string value of the empty attribute also results in true as the attribute missing from the start would do.
For safety reasons, the attribute value could be normalised with fn:normalize-space() in advance in order to exclude the possibility that it only consists of space characters:
not(boolean) => boolean
fn:not($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