Example: Construct an XML document with constant values

This simple example shows how you can construct constant XML values suitable for publishing with SQL/XML publishing functions.

As a simple example, consider the following XML element:
<elem1 xmlns="http://posample.org" id="111">
  <!-- example document -->
  <child1>abc</child1>
  <child2>def</child2>
</elem1>
The document consists of:
  • three element nodes (elem1, child1, and child2)
  • a namespace declaration
  • an "id" attribute on <elem1>
  • a comment node
To construct this document, perform the following steps:
  1. Create an element node named "elem1", using XMLELEMENT.
  2. Add a default namespace declaration to the XMLELEMENT function call for <elem1>, using XMLNAMESPACES.
  3. Create an attribute named "id" using XMLATTRIBUTES, placing it after the XMLNAMESPACES declaration.
  4. Create a comment node using XMLCOMMENT, within the XMLELEMENT function call for <elem1>.
  5. Create a forest of elements that are named "child1" and "child2" using the XMLFOREST function, within the XMLELEMENT function call for <elem1>.
These steps are combined into the following query:
VALUES XMLELEMENT (NAME "elem1",
                   XMLNAMESPACES (DEFAULT 'http://posample.org'),
                   XMLATTRIBUTES ('111' AS "id"),
                   XMLCOMMENT ('example document'),
                   XMLFOREST('abc' as "child1",
                             'def' as "child2"))