DB2 10.5 for Linux, UNIX, and Windows

Namespace declaration

A namespace declaration in the query prolog declares a namespace prefix and associates the prefix with a namespace URI. An association between a prefix and a namespace URI is called a namespace binding. A namespace that is bound in a namespace declaration is added to the statically known namespaces. The statically known namespaces consist of all of the namespace bindings that can be used to resolve namespace prefixes during the processing of a query.

The namespace declaration is in scope throughout the query in which it is declared, unless the declaration is overridden by a namespace declaration attribute in a direct element constructor. Multiple declarations of the same namespace prefix in the query prolog result in an error.

Syntax

Read syntax diagramSkip visual syntax diagram
>>-declare--namespace--prefix--=--URILiteral--;----------------><

prefix
Specifies a namespace prefix that is bound to the URI that is specified by URILiteral. The namespace prefix is used in qualified names (QNames) to identify the namespace for an element, attribute, data type, or function.

The prefixes xmlns and xml are reserved and cannot be specified as prefixes in namespace declarations.

URILiteral
Specifies the URI to which the prefix is bound. URILiteral must be a non-zero-length literal string that contains a valid URI.

Example

The following query includes a namespace declaration that declares the namespace prefix ns1 and associates it with the namespace URI http://posample.org:

declare namespace ns1 = "http://posample.org";
<ns1:name>Thermal gloves</ns1:name>

When the query in the example executes, the newly created node (an element node called name) is in the namespace that is associated with the namespace URI http://posample.org.

Predeclared namespace prefixes

XQuery has several predeclared namespace prefixes that are present in the statically known namespaces before each query is processed. You can use any of the predeclared prefixes without an explicit declaration. The predeclared namespace prefixes for DB2® XQuery include the prefix and URI pairs that are shown in the following table:

Table 1. Predeclared namespaces in DB2 XQuery
Prefix URI Description
xml http://www.w3.org/XML/1998/namespace XML reserved namespace
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 XQuery type namespace
db2-fn http://www.ibm.com/xmlns/prod/db2/functions DB2 function namespace

You can override predeclared namespace prefixes by specifying a namespace declaration in a query prolog. However, you cannot override the URI that is associated with the prefix xml.