NULL literal in XPath

2019-09-05 18:22发布

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条回答
萌系小妹纸
2楼-- · 2019-09-05 18:40

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).

查看更多
登录 后发表回答