xquery to check if the value is null then return null as string

8.9k Views Asked by At

I am new to XQuery. Please guide me to solve the issue below I want to return the null value as a string, if the below expression does not give any value.

Currently, the output doesn't show the 'name' field itself. I want to have a name with null. for eg-

if (IsNull(expression),null,expression)

$output.dataAccessResponse[1]/*:row/*:name/text()
1

There are 1 best solutions below

5
On

You could use the fn:exists() function to test whether or not there is a text() node.

exists($output.dataAccessResponse[1]/:row/:name/text())

You could also use the fn:boolean() function to test the effective boolean value of the node.

boolean($output.dataAccessResponse[1]/:row/:name/text())

If you wanted to test whether or not there was a significant value i.e. something other than whitespace, you can fn:normalize-space() in a predicate, to ensure that only text() nodes that have meaningful text are selected, and then test fn:exists().

exists($output.dataAccessResponse[1]/:row/:name/text()[normalize-space()])

XQuery doesn't have null, so if you are asking what to return to indicate null, then you would want to return an empty sequence () instead of null.

So, you could execute something like this:

let $name := $output.dataAccessResponse[1]/:row/:name/text()
return 
  if (fn:exists($name)) 
  then $name 
  else ()

But at that point, it's really the same as just attempting to select the text() with that XPath and it will either return the text() node or an empty sequence:

$output.dataAccessResponse[1]/:row/:name/text()