Namespace declarations in XML index definitions
In the XMLPATTERN clause of the CREATE INDEX statement, you can specify an optional namespace declaration that maps a URI to a namespace prefix. Then you can use the namespace prefix when you refer to element and attribute names in the XML pattern expression.
Example: Suppose that you want to create an index for documents that look like this:
<customerinfo xmlns="http://posample.org" Cid="1000">
<name>Kathy Smith</name>
<addr country="Canada">
<street>5 Rosewood</street>
<city>Toronto</city>
<prov-state>Ontario</prov-state>
<pcode-zip>M6W-1E6</pcode-zip>
</addr>
<phone type="work">416-555-1358</phone>
</customerinfo>
In a CREATE INDEX statement, use a namespace declaration to map the namespace URI http://posample.org to the character m, and qualify all elements with that namespace prefix:
CREATE INDEX CUST_PHONE_XMLIDX on CUSTOMER(INFO)
GENERATE KEY USING XMLPATTERN
'declare namespace m="http://posample.org";
/m:customerinfo/m:phone/@type' AS SQL VARCHAR(12)
You can include multiple namespace declarations in the same XMLPATTERN expression, but the namespace prefix must be unique within the list of namespace declarations. In addition, you can declare a default namespace for elements that do not have a prefix. If you do not explicitly specify a namespace or namespace prefix for an element, DB2® uses the default namespace. You can declare only one default namespace. If you do not specify a default namespace, the namespace is no namespace.
Default namespace declarations do not apply to attributes.
Example: Write a CREATE INDEX statement to use a default namespace of http://posample.org for all elements:
CREATE INDEX CUST_PHONE_XMLIDX on CUSTOMER(INFO)
GENERATE KEY USING XMLPATTERN
'declare default element namespace "http://posample.org";
/customerinfo/phone/@type' AS SQL VARCHAR(12)
The namespace for the @type attribute is no namespace. If you want to qualify @type, you need to do that explicitly, as shown below.
Example: Suppose that column INFO in table CUSTOMER contains documents of this form:
<customerinfo xmlns:n="http://posample.org"
xmlns="http://posample.org" Cid="1010">
<name>Christine Haas</name>
<addr country="United States">
<street>1000 Oakwood</street>
<city>Toledo</city>
<prov-state>Ohio</prov-state>
<pcode-zip>43537</pcode-zip>
</addr>
<phone n:type="work">567-555-1469</phone>
</customerinfo>
You need an index that looks like this to match those documents:
CREATE INDEX CUST_PHONE_XMLIDX on CUSTOMER(INFO)
GENERATE KEY USING XMLPATTERN
'declare default element namespace "http://posample.org";
declare namespace m="http://posample.org";
/customerinfo/phone/@m:type' AS SQL VARCHAR(12)
The namespace prefix that you use to create an the index does not need to match the namespace prefix that you use in XML documents. However, the fully-expanded QNames must match.