XSLT Reference
not()
Returns true if its boolean argument is false, and false if it is true — the logical negation of a boolean expression.
not(boolean)Description
not() returns the logical negation of its argument. The argument is first converted to a boolean using the same rules as boolean(), and the result is the opposite value.
It is one of the most frequently used functions in XPath predicates and xsl:if conditions, allowing you to express “if this node does not exist”, “if this string is empty”, or “if this condition does not hold”.
Because not() accepts any type and coerces it to boolean, you can negate node-set tests, string emptiness checks, and numeric comparisons in a single, readable expression.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
boolean | any | Yes | The value to negate. Converted to boolean before negation. |
Return value
xs:boolean — true if the argument converts to false, false if it converts to true.
Examples
Skip elements without a required attribute
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product id="1" name="Widget" price="9.99"/>
<product id="2" name="Gadget"/>
<product id="3" name="Doohickey" price="4.49"/>
</products>
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/products">
<priced>
<xsl:for-each select="product[not(@price)]">
<missing id="{@id}"><xsl:value-of select="@name"/></missing>
</xsl:for-each>
</priced>
</xsl:template>
</xsl:stylesheet>
Output:
<priced>
<missing id="2">Gadget</missing>
</priced>
Conditional output based on element absence
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<order>
<item>Book</item>
<item>Pen</item>
</order>
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/order">
<result>
<xsl:if test="not(discount)">
<message>No discount applied.</message>
</xsl:if>
<xsl:for-each select="item">
<line><xsl:value-of select="."/></line>
</xsl:for-each>
</result>
</xsl:template>
</xsl:stylesheet>
Output:
<result>
<message>No discount applied.</message>
<line>Book</line>
<line>Pen</line>
</result>
Notes
not(condition)is equivalent to writingcondition = false()but is more idiomatic and concise.- To negate a compound condition, combine
not()withand/or:not(a or b)means neitheranorbis true. not()cannot be used as a shorthand for inequality (!=). Use@attr != 'value'rather thannot(@attr = 'value')when comparing against multiple nodes, because the semantics differ for node-sets with more than one node.- In XSLT 2.0+ the function works identically; the argument may also be an empty sequence (which converts to
false, sonot(())returnstrue).