XML namespaces and qualified names in DB2 XPath

DB2® XPath uses XML namespaces to prevent naming collisions. An XML namespace is a collection of names that is identified by a namespace URI. Namespaces provide a way of qualifying names that are used for elements, attributes, data types, and functions in XPath.

Names in XPath are called QNames (qualified names) and conform to the syntax that is defined in the W3C Recommendation Namespaces in XML. A QName consists of an optional namespace prefix and a local name. The namespace prefix, if present, is bound to a URI and provides a shortened form of the URI. During query processing, DB2 XPath expands the QName by resolving the URI that is bound to the namespace prefix. The expanded QName includes the namespace URI and a local name. Two QNames are equal if they have the same namespace URI and local name. This means that two QNames can match even if they have different prefixes provided that the prefixes are bound to the same namespace URI.

Using QNames in XPath allows expressions to refer to element types or attribute names that have the same local name, but might be associated with different DTDs or XML Schemas. In the following XML data, pfx1 is a prefix that is bound to some URI. pfx2 is a prefix that is bound to a different URI. c is the local name for all three elements:
<a xmlns:pfx1="uri1" xmlns:pfx2="uri2">
<b>
 <pfx1:c>C</pfx1:c>
 <pfx2:c>B</pfx2:c>
 <c>A</c>
</b>
</a>
The elements in this example share the same local name, c, but naming conflicts do not occur because the elements exist in different namespaces. During expression processing, the name pfx1:c is expanded into a name that includes the URI bound to pfx1 (uri1) and the local name, c. Likewise, the name pfx2:c is expanded into a name that includes the URI bound to pfx2 (uri2) and the local name, c. The element c, which has an empty prefix, is bound to the default element namespace because no prefix is specified. An error is raised if a name uses a prefix that is not bound to a URI.

The namespace prefix must be an NCName (non-colonized name). An XML NCName is similar to an XML Name except that NCName cannot include a colon.

Some namespaces are predeclared; others can be added through declarations in the XPath expression prolog. DB2 XPath includes the following predeclared namespace prefixes:

Prefix URI Description
xs http://www.w3.org/2001/XMLSchema XML Schema namespace
xsi http://www.w3.org/2001/XMLSchema-instance XML Schema instance namespace
fn http://www.w3.org/2005/xpath-functions Default function namespace
xdt http://www.w3.org/2005/xpath-datatypes XPath type namespace
db2-fn http://www.ibm.com/xmlns/prod/db2/functions DB2 function namespace
In addition to the predeclared namespaces, namespaces can be provided in the following ways:
  • The following namespace information is available in the static context:
    • In-scope namespaces are a set of prefix and URI pairs. The in-scope namespaces are used for resolving prefixes that are used in QNames in an XPath expression. In-scope namespaces come from the following sources:
      • Namespace declarations in an XPath expression
      • The XMLNAMESPACES DB2 built-in function in the XMLELEMENT, XMLFOREST, or XMLTABLE DB2 built-in function
    • Default element or type namespace is the namespace that is used for any unprefixed QName that appears where an element or type name is expected. The initial default element or type namespace is the default namespace that is provided by a declare default element namespace clause in the prolog of an XPath expression.
    • Default function namespace is the namespace that is associated with built-in functions: http://www.w3.org/2003/11/xpath-functions. There are no user-defined functions in XPath.