XPath's confusing not() function - inverting a boolean field - Jimmy Rishe
in

InfoPath Dev

Jimmy Rishe

XPath's confusing not() function - inverting a boolean field

If you've ever tried to toggle a boolean field back and forth in an InfoPath form, or perform some behavior based on the inverse of a field's value, you may have encountered a problem I found.  The not() function does not behave the way one would intuitively expect.  The online InfoPath function reference describes the function as follows:

not

Returns true if the argument is false or null. Returns false if the argument is true or not null.

So what happens when a field is false and not null?

In actuality, any time the not() function contains a reference to a field, it returns true if the field exists, and false if it does not exist, regardless of its value.

To demonstrate this, I've created an InfoPath form with a boolean field in an optional section, and set up a rule and expression box like this:

When I preview the form, it exhibits the following behavior:

not(field1) is only true when field1 is non-existent, and when field1 has a value, not(field1) is always false.
What's more, the button changes the value of field1 from true to false, but not from false to true. It's only half of a toggle button!

The solution:

Despite all this, we can obtain the inverse of a boolean field's value with a little finagling.

The XPath expression field1 = "true" will have the value "false" when field1 is false, and "true" when field1 is true; in other words, it will always follow field1's value whenever field1 exists.  We can conclude that not(field1 = "true") will be the inverse of field1's value, and that is certainly the case. But if we are going to use an equality expression to determine field1's value, we may as well ditch the not() function entirely, and just use the expression field1 = "false", which will be "true" when field1 is false, and "false" when field1 is true.  Here are the new settings for the expression box and the button's rule:

And when I preview the form, I see this behavior:

Both expressions have the inverse of the value of field1 when it exists, and the button correctly toggles field1's value. One final note is that the two expressions have different values when field1 does not exist.

 

Comments

 

jerkham said:

While this is a fairly simple concept, it's not intuitive.  Very glad to find this information.  Thanks!

June 29, 2010 9:26 PM
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.