Namespaces for XML data sources

Use the NAMESPACES nickname column option to identify the elements or attributes that are part of a namespace.

You can specify the NAMESPACES nickname option when you register nicknames. The value of the NAMESPACES nickname column option is a comma-separated list of name and value pairs. The XML wrapper uses the name and value pairs to resolve the namespace prefixes that are in the nickname XPath expressions. The prefixes that are used in the XPath expressions are processed by the XPath processor.

In the following example, the XML document includes the name, code, and description information for three products. The XML document declares two namespaces, http://www.one.com and http://www.two.com, and has one default namespace http://www.default.com. The product element is associated with the ns1 namespace. The product element contains the name and code attributes and the desc element. The name attribute is not associated with a namespace. The code attribute is associated with the ns2 namespace. The desc element is associated with the default namespace.

<?xml version="1.0" encoding="UTF-8"?>
<doc xmlns:ns1="http://www.one.com" xmlns:ns2="http://www.two.com"
    xmlns="http://www.default.com">
<ns1:product name="Computer" ns2:code="ABC123" 
    <desc>"The Computer product description"</desc>
<ns1:product name="Keyboard" ns2:code="EFG456" 
    <desc>"The Keyboard product description"</desc>
<ns1:product name="Mouse" ns2:code="HIJ789" 
    <desc>"The Mouse product description"</desc>
</ns1:product>
</doc>

The following table shows the namespace that is associated with each element and attribute in the XML document.

Table 1. The elements, attributes, and namespaces in the XML document
Element or attribute Namespace in XML document Notes
product: An element in the XML document. ns1="http://www.one.com" None
name: An attribute of the product element in the XML document. None The name attribute is not associated with a namespace.
code: An attribute of the product element in the XML document. ns2="http://www.two.com" None
desc: An element within the product element in the XML document. "http://www.default.com" The desc element uses the default namespace, which does not contain a prefix.
When you register the nickname for the XML document, you define three columns to correspond to the elements and attributes in the XML document. You specify the namespace information in the NAMESPACES nickname option. For example:
CREATE NICKNAME products    
   (name VARCHAR(16) OPTIONS (XPATH '@name'),
    code VARCHAR(16) OPTIONS (XPATH '@pre2:code')
    description VARCHAR (256) OPTIONS (XPATH './default:desc'))
    FOR SERVER xml_server
    OPTIONS (FILE_PATH '/home/mbreining/sql/xml/namespaces.xml',
        XPATH '//pre1:name',
        NAMESPACES 'pre1="http://www.one.com", pre2="http://www.two.com",
           default="http=//www.default.com"');   

The following table shows how the XML document namespaces correspond to the values that you specify in the CREATE NICKNAME statement.

Table 2. XML document namespaces and corresponding values from the CREATE NICKNAME statement.
Namespace in XML document Column name in the CREATE NICKNAME statement Value in the XPATH column option Value in the NAMESPACES nickname option
The namespace is ns1="http://www.one.com". None None The value is pre1="http://www.one.com". The value is a prefix that you define (pre1) and the unique identifier for the namespace ("http://www.one.com").
None. The attribute is not associated with a namespace. Name The value is @name. The value is an attribute that is in the XML document (name). None
The namespace is ns2="http://www.two.com" Code The value is @pre2:code. The value is a prefix that you define (pre2) and an attribute that is in the XML document (code). The value is pre2="http://www.two.com". The value is a prefix that you define (pre2) and the unique identifier for the namespace ("http://www.two.com").
The namespace is "http://www.default.com" The default namespace does not contain a prefix. Description The value is ./default:desc. The value is a prefix that you define (default) and an element that is in the XML document (desc). The value is default="http=//www.default.com". The value is a prefix that you define (default) and the unique identifier for the namespace ("http://www.default.com").

The NAMESPACES nickname option uses packed descriptors to support strings that are greater than 256 characters.

For more information about XML namespaces, see the explanation for namespaces on the W3C Web site.