Definition of default XML namespaces

You define a default namespace in an element of an XML document with an attribute of the form xmlns="URI". In the following example, a document has a default namespace bound to the URI http://www.iAnywhere.com/EmployeeDemo:

<x xmlns="http://www.iAnywhere.com/EmployeeDemo"/>

If the element does not have a prefix in its name, a default namespace applies to the element and to any descendant of that element where it is defined. A colon separates a prefix from the rest of the element name. For example, <x/> does not have a prefix, while <p:x/> has the prefix p. You define a namespace that is bound to a prefix with an attribute of the form xmlns:prefix="URI". In the following example, a document binds the prefix p to the same URI as the previous example:

<x xmlns:p="http://www.iAnywhere.com/EmployeeDemo"/>

Default namespaces are never applied to attributes. Unless an attribute has a prefix, an attribute is always bound to the NULL namespace URI. In the following example, the root and child elements have the iAnywhere1 namespace while the x attribute has the NULL namespace URI and the y attribute has the iAnywhere2 namespace:

<root xmlns="iAnywhere1" xmlns:p="iAnywhere2">
<child x='1' p:y='2' />
</root>

The namespaces defined in the root element of the document are applied in the query when you pass an XML document as the namespace-declaration argument of an openxml query. All parts of the document after the root element are ignored. In the following example, p1 is bound to iAnywhere1 in the document and bound to p2 in the namespace-declaration argument, and the query is able to use the prefix p2:

SELECT * 
FROM openxml('<p1:x xmlns:p1="iAnywhere1">123</p1:x>', '/p2:x', 1, '<root xmlns:p2="iAnywhere1"/>')
WITH ( c1 int '.' );

When matching an element, you must correctly specify the URI that a prefix is bound to. In the example above, the x name in the xpath query matches the x element in the document because they both have the iAnywhere1 namespace.

When matching an element, you must correctly specify the URI that a prefix is bound to. In the example above, the x name in the xpath query matches the x element in the document because they both have the iAnywhere1 namespace. The prefix of the xpath element x refers to the namespace iAnywhere1 defined within the namespace-declaration that matches the namespace defined for the x element within the xml-data.

Do not use a default namespace in the namespace-declaration of the openxml system procedure. Use a wildcard query of the form /*:x, which matches an x element bound to any URI including the NULL namespace, or bind the URI you want to a specific prefix and use that in the query,

 See also