XML input and output overview

The Db2® database server, which manages both relational and XML data, offers various methods for the input and output of XML documents.

XML documents are stored in columns defined with the XML data type. Each row of an XML column stores a single well-formed XML document. The stored document is kept in its hierarchical form, preserving the XML data model; the document is not stored as text or mapped to a different data model.

XML columns can be defined in tables that contain columns of other types, which hold relational data, and multiple XML columns can be defined for a single table.

Input

Figure 1 shows the various ways that XML data can be put into the database system.
Figure 1. Methods for input of XML data
XML data input occurs through insert, update, load, import, or decomposition.

Which input method you use depends on the task you wish to accomplish:

Insert or update
Well-formed documents are inserted into XML columns using the INSERT SQL statement. A document is well-formed when it can be parsed successfully. Validation of the XML documents during an insert or update operation is optional. If validation is performed, the XML schema must first be registered with the XML schema repository (XSR). Documents are updated using the UPDATE SQL statement, or using XQuery updating expressions.
Annotated XML schema decomposition
Data from XML documents can be decomposed or stored into relational and XML columns using annotated XML schema decomposition. Decomposition stores data in columns according to annotations that are added to XML schema documents. These annotations map the data in XML documents to columns of tables.

XML schema documents referenced by the decomposition feature are stored in the XML schema repository (XSR).

Import
XML documents can be imported into XML columns using the import utility. Validation of the XML documents being imported is optional. If validation is performed, the XML schema against which the documents are validated, must first be registered with the XML schema repository (XSR).
XML schema repository (XSR) registration
The XML schema repository (XSR) stores XML schemas that are used for the validation or decomposition of XML documents. Registration of XML schemas is usually a prerequisite for other tasks that are performed on XML documents which have a dependency on these schemas. XML schemas are registered with the XSR using stored procedures or commands.

Output

Figure 2 shows the various ways that XML data can be retrieved from the database system.
Figure 2. Methods for output of XML data
XML data is retrieved with XQuery, SQL, or the export utility.

Which output method you use depends on the task you wish to accomplish:

XQuery
XQuery is a language that enables you to query within XML documents. It addresses specific requirements of querying XML data that is highly variable in structure, unlike queries on relational data that expect a predictable structure.

XQuery can be invoked on its own, or it can invoke SQL to query XML stored in a Db2 database, through the db2-fn:xmlcolumn and db2-fn:sqlquery XQuery functions. db2-fn:xmlcolumn retrieves an entire XML column, whereas db2-fn:sqlquery retrieves XML values that are based on an SQL fullselect.

SQL
When querying XML data using an SQL fullselect, the query occurs at the column level. For this reason, only entire XML documents can be returned from the query; it is not possible to return fragments of an XML document using only SQL. To query within XML documents, XQuery must be used. XQuery can be invoked from SQL using the XMLQUERY or XMLTABLE SQL/XML functions, or the XMLEXISTS predicate. The XMLQUERY function returns the result of an XQuery expression as an XML sequence. The XMLTABLE function returns the result of an XQuery expression as a table. The XMLEXISTS SQL predicate determines whether an XQuery expression returns a non-empty sequence.

A number of publishing functions are also available to construct XML values from XML data stored in Db2 database server. XML values constructed with these publishing functions do not have to be well-formed XML documents.

Export
XML documents can be exported from XML columns using the export utility. Exported XML data is stored separately from the exported relational data in the main data file. Details about each exported XML document is not stored directly in the main exported data file. The details are instead represented in the main data file by an XML data specifier (XDS).