2

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()
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
gunjan gupta
  • 51
  • 1
  • 12

1 Answers1

4

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() 
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • thanks for the quick response.but i am looking for if statement.te code should return the original value if exists else empty sequence ().Exists would return true/false. – gunjan gupta Jun 05 '20 at 14:41
  • okay, it wasn't quite clear what you were asking for. Just address the `text()` node with the XPath and it will either return the node, or an empty sequence. No need for if/else blocks at all. – Mads Hansen Jun 05 '20 at 14:59
  • Since name is null,i am getting THIS IS TEST MESG N .however i am expecting the output as THIS IS TEST MESG N – gunjan gupta Jun 05 '20 at 15:25
  • i have given the xpath to text already.$output.dataAccessResponse[1]/:row/:name/text() – gunjan gupta Jun 05 '20 at 15:26
  • If you want to copy the name element, then select the name element. It might be helpful if you showed more of the code for context and to see how this is being evaluated. – Mads Hansen Jun 05 '20 at 15:32