简体   繁体   中英

Using XQuery to remove the xsi:nil = true when a value is assigned to an element node

Can someone help me to achieve the folowing:

  • How to delete in XQuery the attribute xsi:nil= "true", when the element has an assigned value?

In case the element has a null value then xsi:nil= "true" is kept.

Thnx.

You can use XQuery Update to achieve this, eg like this:

declare variable $e := <entry nil="true" />;

if ($e/@nil != "null") then (
copy $c := $e
modify (
  delete node $c/@nil
)
return $c
)
else $e

Please note, that there is no actual null value in xquery as you might be familar with from languages like Java. Therefore I used a simple string here to indicate a null-value, you might want to change this.

In addition, please be aware that this function does not return the same node without the attribute, but instead it copies the node and returns this new modified node without the nil attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM