简体   繁体   中英

fn:boolean What does it for?

I have already read some articles but I am still confused about the "fn:boolean"in XQuery3.0...Is this a function to This is where I look at: http://www.xqueryfunctions.com/xq/fn_boolean.html

For instance: If I put fn:boolean ((1,2) > (5000,6,1)), it is true.

If I put fn:boolean ((6,1) > (1,99,22)), it is true as well!

I am really confused about this function even though the article online stating that this function is rarely called but I still want to figure out what does it for...

The function fn:boolean(X) returns the effective boolean value of X. For example, the effective boolean value of 1 is true, 0 is false, "true" is true, "" is false, "false" is (wait for it) true. The EBV of a node (or a non-empty node-sequence) is true, which is what makes expressions like X[following-sibling::X] work.

The effective boolean value of a boolean is the boolean unchanged: and that's the case with your example.

There are two cases where a call to fn:boolean is pointless:

  • where the argument to the function is already a boolean, as in your example

  • where the call appears in a context, such as the condition of an XPath 2.0 "if" expression, or a predicate in square brackets, or the test attribute of xsl:if , where there is in effect an implicit call on fn:boolean() already.

In other contexts, for example where you are supplying an argument to a function or binding the value of an XSLT or XQuery variable, calling fn:boolean explicitly can be useful to force the value to the right type. It can also help the reader of your code to understand that there is a type conversion happening.

Well, what do you think is the result of (6,1) > (1,99,22) in XQuery? That gives a boolean value already so maybe you should start figuring out to understand comparison of sequences first: https://www.w3.org/TR/xquery-31/#id-general-comparisons says "The result of the comparison is true if and only if there is a pair of atomic values, one in the first operand sequence and the other in the second operand sequence, that have the required magnitude relationship". So as 6 from the first sequence on the left is greater than 1 in the second sequence the comparison gives true.

Calling boolean then on that value does not change that as that function for boolean values does not give anything but the boolean value passed in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM