Calling Boolean Functions

This section describes the Boolean functions that you can call in a query. The operations you can perform are

Converting an Object to Boolean

In some situations, you might want to force a Boolean comparison. The XPath processor performs a Boolean comparison if either operand is a Boolean value. Consequently, if neither operand is a Boolean value, call the boolean() function on one operand to convert it to a Boolean value. The XPath processor automatically converts the other operand to a Boolean value. The format of the boolean() function is

boolean boolean(object) 
       

    

The boolean() function converts its argument to Boolean as follows:

  • A number is false if and only if it is one of the following:
    • Positive zero
    • Negative zero
    • NaN (not a number)
  • A node set is false if and only if it is empty.
  • A string is false if and only if its length is 0.

The boolean() function is useful in comparisons. For example, the following query returns b elements that either contain both c and d elements as children or contain neither c nor d elements as children:

/a/b[boolean(c) = d]
               

            

This query is equivalent to the following query:

/a/b [(c and d) or (not(c) and not(d))]
               

            

Obtaining Boolean Values

To obtain the opposite Boolean value, call the not() function. The format is

boolean not(boolean) 
               

            

The not() function returns true if its argument is false, and returns false if its argument is true. For example, the following query finds all authors who have publications but no degrees or awards:

author[not(degree or award) and publication] 
               

            

To obtain the value true, call the true() function. The format is

boolean true() 
               

            

The true() function returns true.

To obtain the value false, call the false() function. The format is

boolean false() 
               

            

The false() function returns false.

Determining the Context Node Language

To determine whether the language of the context node is the language you expect it to be, call the lang() function. The format is

boolean lang(string) 
               

            

The lang() function returns true or false depending on whether the language of the context node as specified by the xml:lang attribute is the same as, or is a sublanguage of, the language specified by the argument string. The language of the context node is determined by the value of the xml:lang attribute on the context node or, if the context node has no xml:lang attribute, by the value of the xml:lang attribute on the nearest ancestor of the context node that has an xml:lang attribute.

If there is no such attribute, then lang() returns false. If there is such an attribute, lang() returns true in the following situations:

  • The attribute value is equal to the argument string.
  • The attribute value has a suffix starting with a dash (-) such that the attribute value is equal to the argument string if you ignore the suffix.

In both situations, case is ignored. For example:

lang("en")
       

    

This returns true if the context node is any of these elements:

  • <para xml:lang="en"/>
  • <div xml:lang="en"><para/></div>
  • <para xml:lang="EN"/>
  • <para xml:lang="en-us"/>
 
Free Stylus Studio XML Training: