Wednesday, August 18, 2010

How to check if the xml node exists or variable has a null value in xquery

 

There are three ways you can do it.

1) fn:exists(argument as item()*) – if argument returns empty then its false otherwise true.

for eg.

-----------

fn:exists(xdmp:eval(“1+1”))

returns true.

------------

fn:exists(xdmp:eval(“”))

returns false.

-------------

declare variable $var := <node><elem></elem></node>;

fn:exists($var//elem)

returns true.

fn:exists($var//para)

returns false.

-----------------

 

2) xdmp:exists(searchable-expression as item()*) – returns true if the result of the expression is xpath node()*

for eg.

xdmp:exists(fn:doc())

 

3) If (expression) - use if statement to check if the xpath returns a result.

 

for eg.

declare variable $var := <node><elem></elem></node>;

if($var//elem)

then <result>$var//elem/@value</result>

else <result>empty</result>

No comments:

Post a Comment