Modifying data

8.8 Modifying data

Storing XML documents inside a database is an important aspect of data management. It is a requirement of today’s business needs to be compliant with standards, and should be able cater to the changing needs of customers in a timely manner. DB2 not only provides for efficient storage of XML documents, with its native storage technology, but also provides ways to manipulate the XML data being stored. It provides various functions like XMLPARSE and XMLSERIALIZE, to convert the text presentation of XML documents into the native XML format and vice versa

8.8.1 XMLPARSE

As mentioned above XMLPARSE is a function that parses the given XML text and then converts it into native XML format (i.e. hierarchical tree format). While inserting DB2 by default does an implicit parse of the XML text, but if the user wants to explicitly parse the XML text, then the XMLPARSE function can be used.

For example, the following INSERT statement first parses the given XML text and if it finds that the XML document is well-formed, it goes ahead with insertion of the XML document into the specified column of the table.

INSERT INTO PRODUCT(PID,DESCRIPTION) VALUES(‘100-345-01’, XMLPARSE( DOCUMENT ‘<product xmlns="http://posample.org" pid="100-100-01">

<description><name>Snow Shovel, Basic 22 inch</name> <details>Basic Snow Shovel, 22 inches wide, straight handle with D-

Grip</details> <price>9.99</price> <weight>1 kg</weight> </description> </product> ‘))

Another reason for using XMLPARSE is that it allows one to specify the way whitespace is to be treated. By default DB2 strips boundary whitespace between elements, but if the user wants to preserve the whitespace, then the PRESERVE WHITESPACE clause can be used as shown below.

Chapter 8 – Query languages for XML 213 INSERT INTO PRODUCT(PID,DESCRIPTION) VALUES(‘100-345-01’, XMLPARSE(

DOCUMENT ‘<product xmlns="http://posample.org" pid="100-100-01"> <description><name>Snow Shovel, Basic 22 inch</name> <details>Basic Snow Shovel, 22 inches wide, straight handle with D-

Grip</details> <price>9.99</price> <weight>1 kg</weight> </description> </product> ‘ PRESERVE WHITESPACE))

8.8.2 XMLSERIALIZE

As the name suggests, the function XMLSERIALIZE is used to serialize the XML tree structure into string/binary format. It allows the XML tree format to be serialized into CHAR/CLOB/BLOB formats. This type of serialization is necessary when you need to manipulate the XML in text format, because any data returned by XQuery from the set of XML documents is by default of type XML.

For example, the following SELECT statement will fetch XML documents from the PRODUCT table’s DESCRIPTION column and return them as serialized character large objects (CLOB) belonging to the product with pid ‘10010001’.

SELECT XMLSERIALIZE(DESCRIPTION AS CLOB(5K)) FROM PRODUCT WHERE PID LIKE ‘10010001’

Execution Result: 1

------------------------------------------------------------------------ <product xmlns="http://posample.org" pid="100-100-01"> <description><name>Snow Shovel, Basic 22 inch</name> <details>Basic Snow Shovel, 22 inches wide, straight handle with D-

Grip</details><price>9.99</price><weight>1 kg</weight> </description> </product>

1 record(s) selected.

8.8.3 The TRANSFORM expression

In DB2 9.5, the TRANSFORM expression was introduced, which allows the user to make modifications to existing XML documents stored in XML columns. The transform expression is part of the XQuery Update facility which facilitates the following operations on an XDM instance.

 Insertion of a node  Deletion of a node  Modification of a node by changing some of its properties while preserving its

identity

Database Fundamentals 214  Creation of a modified copy of a new node with a new identity.

Given below is an example using transform expression that updates the XML document of customer having cid=1000. The transform expression is updating both the text value of phone number element and the value of attribute ‘type’ with ‘home’.

The XML document before execution of the update statement: <customerinfo>

<name>John Smith</name> <addr country=“Canada">

<street>Fourth</street> <city>Calgary</city> <state>Alberta</state> <zipcode>M1T 2A9</zipcode>

</addr>

<phone type="work">963-289-4136</phone>

</customerinfo> The update statement with transform expression:

update customer set info = xmlquery( 'copy $new := $INFO

modify ( do replace value of $new/customerinfo/phone with "416-123-4567", do replace value of $new/customerinfo/phone/@type with "home" )

return $new') where cid = 1000;

The XML document after execution of the update statement: <customerinfo>

<name>John Smith</name> <addr country=“Canada">

<street>Fourth</street> <city>Calgary</city> <state>Alberta</state> <zipcode>M1T 2A9</zipcode>

</addr>

<phone type="work">963-289-4136</phone>

</customerinfo>