问题
Is there a NULL literal in XPath 1.0 or 2.0?
My use case is that I have a conditional (if then else
) XPath expression and I want to return NULL to signify a certain condition. I am afraid that returning an empty string might be ambiguous in my case as it could be a valid result of the other part of the if then else
expression.
回答1:
The empty sequence ()
can be used as such. It is also returned if there is no result for a path expression.
let $foo := "foo"
return
if ($foo = ("foo", "bar", "batz")) then
$foo
else
()
You can check for an empty sequence using
let $result := ()
return empty($result)
If you pass the result of the first XPath expression to your native code, you should be able to distinguish "NULL" from the empty string by having no results (empty sequence / "NULL") or having a result string (which could be empty).
来源:https://stackoverflow.com/questions/17374407/null-literal-in-xpath