XML namespace declarations

Qualified XML names (QNames) are used to define the element and attribute tags in XML pattern expressions. The qualifier for a QName is a namespace prefix which has been associated with a namespace URI.

The XML pattern can be specified using an optional namespace declaration to map a namespace prefix to a namespace URI string literal or to define a default namespace URI for the XML pattern. The namespace prefix is then used to qualify names of elements and attributes in the XML pattern to match them with the namespace used in the document.

In the following example, the shorthand namespace prefix m is mapped to http://www.mycompanyname.com/

CREATE INDEX empindex on department(deptdocs)
     GENERATE KEYS USING XMLPATTERN 
     'declare namespace m="http://www.mycompanyname.com/";
     /m:company/m:emp/m:name/m:last' AS SQL VARCHAR(30)

Note that if this CREATE INDEX statement is issued from the command line processor (CLP), the embedded semicolon becomes problematic, because the semicolon is the default statement terminator. To avoid this issue, use one of the following workarounds:

  • Ensure the semicolon is not the last non whitespace character on the line (by adding an empty XQuery comment after the semicolon, for example).
  • Change the default statement terminator in the CLP from the command line.

Multiple namespace declarations can also be specified in the same XMLPATTERN expression, but the namespace prefix must be unique within the list of namespace declarations. In addition, the user has the option to declare a default namespace for elements that do not have a prefix. If a namespace or namespace prefix is not explicitly specified for an element, then the default namespace will be used. Default namespace declarations do not apply to attributes. If the user does not specify a default namespace, then the namespace will be no namespace. Only one default namespace can be declared. This namespace declaration behavior follows XQuery rules.

The previous example can also be written using a default namespace:

CREATE INDEX empindex on department(deptdocs)
   GENERATE KEY USING XMLPATTERN
   'declare default element namespace "http://www.mycompany.com/";
    /company/emp/name/last') AS SQL VARCHAR(30)

In the next example, the @id attribute has the no namespace namespace, since the default namespace http://www.mycompany.com/ applies only to the company and emp elements, but not the @id attribute. This follows basic XQuery rules, since in an XML document default namespace declarations do not apply to attributes.

CREATE INDEX empindex on department(deptdocs)
   GENERATE KEY USING XMLPATTERN
   'declare default element namespace "http://www.mycompany.com/";
    /company/emp/@id' AS SQL VARCHAR(30)

Since the @id attribute should have the same namespace as the company and emp elements, the statement could be rewritten as:

CREATE INDEX empindex on department(deptdocs)
   GENERATE KEY USING XMLPATTERN
   'declare default element namespace "http://www.mycompany.com/";
    declare namespace m="http://www.mycompanyname.com/";
    /company/emp/@m:id' AS SQL VARCHAR(30)

The namespace prefix used to create the index and the namespace prefix used in the instance documents do not need to match to be indexed, but the fully expanded QName does need to match. The value of the namespace to which the prefix expands is important, not the prefix name itself. For example, if the namespace prefix for the index is defined as m="http//www.mycompany.com/" and the namespace prefix used in the instance document is c="http//www.mycompany.com/", then c:company/c:emp/@id in the instance documents would be indexed, since both shorthand namespace prefixes m and c expand to the same namespace.