Oracle insertChildXML Function Example

In this post, we will explain the insertChildXML  function in Oracle with a simple example. We will use the XML provided below :

<Title Brand="HP">HP G62X-400 LD644AAR 15.6</Title>
<Title Brand="Sony">Sony VAIO VPCEE45FX/WI 15.5 Laptop</Title>
<Title Brand="HP">HP Pavilion dv7-4165dx 17.3 Notebook PC</Title>
<Title Brand="Toshiba">Toshiba Portege R700-S1312 13.3 Notebook</Title>

My aim would be to INSERT a new set of "Notebook" element into this XML for another laptop brand. The new tags which we would like to insert is shown separately below

<Title Brand="ASUS">Asus N61 16 Notebook </Title>

To easily update the XML for this purpose, we can use the function "insertChildXML" in Oracle. The usage of insertChildXML for this case is illustrated below

SET A.ORDERDATA = INSERTCHILDXML(A.ORDERDATA, /* The column to be updated, in this case ORDERDATA our XMLTYPE column */
                                    '/Laptop',/* The XPath where the insert has to take place, in this case it is from root 'Laptop' node */
                                    'Notebook', /* The element name that needs to be inserted */
                                    XMLTYPE('<Notebook><Title Brand="ASUS">Asus N61 16 Notebook </Title><Price>1700</Price></Notebook>')) /* data */

The output of this SQL is provided below.(I have skipped some sections of the XML to show only the new Notebook element)

<Laptop><Notebook><Title Brand="HP">HP G62X-400 LD644AAR 15.6</Title></Notebook>……..<Notebook><Title Brand="ASUS">Asus N61 16 Notebook </Title><Price>1700</Price></Notebook></Laptop>

Now, for all the Notebook elements if you want to  include a RATING with a default value of 5, how can this be done using INSERTCHILDXML function? The SQL to do this is provided below

'/Laptop/Notebook', /* grab all matching Notebook elements */
'Rating', /* insert a Rating tag under Notebook */
XMLTYPE('<Rating>5</Rating>')) /* set default value for Rating tag */

The output of this SQL is shown below (not all tags are shown)

<Laptop><Notebook><Title Brand="HP">HP G62X-400 LD644AAR 15.6</Title><Price>450</Price><Rating>5</Rating></Notebook><Notebook><Title Brand="Sony">Sony VAIO VPCEE45FX/WI 15.5 Laptop</Title><Price>800</Price><Rating>5</Rating></Notebook>………….</Laptop>

Now, I want to add a DISCOUNT tag to "Notebook" elements, where price of the Notebook is greater than 1000..This can be done as shown below

'/Laptop/Notebook[Price>1000]', /* Pick only those nodes where Price is greater than 1000 */
                                    'Discount', /* Add discount tag selectively */

The output of this SQL is shown below..two Notebook elements are shown, one with Discount added as it mets the condition and the other with no discount added

<Notebook><Title Brand="HP">HP G62X-400 LD644AAR 15.6</Title><Price>450</Price><Rating>5</Rating></Notebook>
<Notebook><Title Brand="Toshiba">Toshiba Portege R700-S1312 13.3 Notebook</Title><Price>1200</Price><Rating>5</Rating><Discount>50</Discount></Notebook>

No comments:

Post a Comment