Delete expression
A delete expression deletes zero or more nodes from a node sequence.
Syntax
- do delete
- The keywords that begin a delete expression.
- TargetExpression
- An XQuery expression that is not an updating expression. If the expression includes a top-level comma operator, the expression must be enclosed in parentheses. The result of TargetExpression must be a sequence of zero or more nodes. Each node's parent property cannot be empty.
The transform expression evaluates the delete expression and generates a list of updates that consist of nodes to be deleted. Any node that matches the TargetExpression is marked for deletion. When deleting the TargetExpression nodes, the nodes are detached from their parent nodes. The nodes and the nodes' children are no longer part of the node sequence.
Examples
The following examples use the CUSTOMER table from the Db2 SAMPLE database. In the CUSTOMER table, the XML column INFO contains customer address and phone information.
The following expression deletes the address element and all of its children nodes, and all phone numbers from the XML document where the phone's attribute type is not home:
xquery
transform
copy $mycust := db2-fn:sqlquery('select INFO from CUSTOMER where Cid =1003')
modify
do delete ($mycust/customerinfo/addr, $mycust/customerinfo/phone[@type!="home"])
return $mycust
When run against the SAMPLE database, the expression returns the following result:
<customerinfo Cid="1003"> <name>Robert Shoemaker</name> <phone type="home">416-555-2937</phone> </customerinfo>
The following example deletes the type attribute from any phone element node when the attribute value is home.
xquery
transform
copy $mycust := db2-fn:sqlquery('select info from customer where cid = 1004')
modify (
for $phone in $mycust/customerinfo//phone[@type="home"]
return
do delete $phone/@type )
return $mycust
When run against the SAMPLE database, the expression returns the following result:
<customerinfo Cid="1004"> <name>Matt Foreman</name> <addr country="Canada"> <street>1596 Baseline</street> <city>Toronto</city> <prov-state>Ontario</prov-state> <pcode-zip>M3Z 5H9</pcode-zip> </addr> <phone type="work">905-555-4789</phone> <phone>416-555-3376</phone> <assistant><name>Gopher Runner</name> <phone>416-555-3426</phone> </assistant> </customerinfo>
The expression deletes the type attribute from both the customer's phone number and the assistant's phone number.