XML namespaces and qualified names in XQuery
XQuery 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 XQuery.
Names in XQuery are called QNames (qualified names) and conform to the syntax that is specified in a recommendation by the World Wide Web Consortium (W3C). 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, XQuery 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.
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 XQuery expression prolog. XQuery 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 | XQuery type namespace |
- 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 XQuery expression.
In-scope namespaces come from the following sources:
- Namespace declarations in an XQuery expression
- The XMLNAMESPACES Db2 built-in
function in the XMLELEMENT or XMLFOREST Db2 built-in
function
If the XMLQUERY Db2 built-in function is an argument to XMLELEMENT or XMLFOREST, the namespaces that are declared in that XMLELEMENT or XMLFOREST invocation become part of the static context for the XQuery expression in the XMLQUERY invocation.
- 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 XQuery 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 XQuery.
- 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 XQuery expression.
In-scope namespaces come from the following sources: