Purpose
This function inserts a specified XML fragment into a target XML document. The insertion position is determined by the nodes indicated by the XPath expression.
Syntax
INSERTCHILDXML( XMLType_instance, XPath_string, child_expr, value_expr [, namespace_string ])
Parameters
| Field | Description |
|---|---|
| XMLType_instance | Specifies an XMLType instance that represents the target XML document. |
| XPath_string | The XPath expression that indicates one or more target nodes where one or more child nodes will be inserted. You can use an absolute path (starting with a slash) or a relative path (omitting the initial slash). If you omit the initial slash, the context for the relative path defaults to the root node. |
| child_expr | Specifies one or more elements or attribute nodes to be inserted. |
| value_expr | Specifies an XMLType fragment that indicates one or more nodes to be inserted. It must resolve to a string or an XMLType. |
| namespace_string | Optional. Provides namespace information for the XPath_string. |
Note
If the parent node has a namespace but the value to be inserted does not, empty namespaces are added during traversal.
Return type
XMLType
Examples
Use the
INSERTCHILDXMLfunction to insert a child node into a target XML document. In this example, the target XML document contains the<bookstore>node and its child nodes. The XPath expression/bookstoreindicates that the child node should be inserted into the<bookstore>node. The name of the child node to be inserted isprice, and its content is an XMLType instance that represents<price>99.9</price>. The query result is renamed toresultusing theASkeyword. Finally, the result is retrieved from thedualtable.SELECT INSERTCHILDXML( XMLtype('<bookstore> <book att="old"></book> <author>carrot</author> </bookstore>'), '/bookstore', 'price', XMLtype('<price>99.9</price>') ) AS result FROM dual;The result is as follows:
+-----------------------------------------------------------------------------------------------+ | RESULT | +-----------------------------------------------------------------------------------------------+ | <bookstore> <book att="old"/> <author>carrot</author> <price>99.9</price> </bookstore> | +-----------------------------------------------------------------------------------------------+ 1 row in setInsert a node.
SELECT INSERTCHILDXML( xmltype( '<bookstore> <book att="old"></book> <author>carrot</author> </bookstore>' ), '/bookstore/book', 'a', xmltype('<a><b/></a>') ) FROM dual;The result is as follows:
+----------------------------------------------------------------------------------------------------------------------------------------------+ | INSERTCHILDXML(XMLTYPE('<BOOKSTORE><BOOKATT="OLD"></BOOK><AUTHOR>CARROT</AUTHOR></BOOKSTORE>'),'/BOOKSTORE/BOOK','A',XMLTYPE('<A><B/></A>')) | +----------------------------------------------------------------------------------------------------------------------------------------------+ | <bookstore> <book att="old"> <a> <b/> </a> </book> <author>carrot</author> </bookstore> | +----------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in setIf the XPath expression returns multiple nodes, all of them will be inserted.
SELECT INSERTCHILDXML( xmltype( '<bookstore> <book att="old"></book> <book></book> <author>carrot</author> </bookstore>' ), '/bookstore/book', 'price', xmltype('<price>99.9</price>') ) FROM dual;The result is as follows:
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | INSERTCHILDXML(XMLTYPE('<BOOKSTORE><BOOKATT="OLD"></BOOK><BOOK></BOOK><AUTHOR>CARROT</AUTHOR></BOOKSTORE>'),'/BOOKSTORE/BOOK','PRICE',XMLTYPE('<PRICE>99.9</PRICE>')) | +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | <bookstore> <book att="old"> <price>99.9</price> </book> <book> <price>99.9</price> </book> <author>carrot</author> </bookstore> | +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in setInsert an attribute.
SELECT INSERTCHILDXML( xmltype( '<bookstore> <book att="old"></book> <author>carrot</author> </bookstore>' ), '/bookstore/book', '@att2', 'abc' ) FROM dual;The result is as follows:
+---------------------------------------------------------------------------------------------------------------------------------+ | INSERTCHILDXML(XMLTYPE('<BOOKSTORE><BOOKATT="OLD"></BOOK><AUTHOR>CARROT</AUTHOR></BOOKSTORE>'),'/BOOKSTORE/BOOK','@ATT2','ABC') | +---------------------------------------------------------------------------------------------------------------------------------+ | <bookstore> <book att="old" att2="abc"/> <author>carrot</author> </bookstore> | +---------------------------------------------------------------------------------------------------------------------------------+ 1 row in set
