1

I encountered a strange behavior on SQL Server 2008 R2 when performing a Xpath query on a XML field with nodes which have an empty namespace.

This query doesn't return results:

[xml_field].query('/RootNode/NodeWithEmptyNamespace')

This query returns results:

[xml_field].query('/dft:RootNode/NodeWithEmptyNamespace')

For clarification this query also returns results so no prefix is mandatory for root node (and probably default namespace):

[xml_field].query('/RootNode')

According to the XML namespace default documation when empty namespace is defined the namespace is none.

The XML in the database is the following:

<RootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org">
  <otherNode>Dummy data</otherNode>
  <NodeWithEmptyNamespace xmlns="">Other dummy data</NodeWithEmptyNamespace>
</RootNode>

The full query:

WITH XMLNAMESPACES ('http://tempuri.org' as dft)
SELECT TOP 150 [ID],
       [xml_field].query('/dft:RootNode/NodeWithEmptyNamespace')
  FROM [database];

Does anyone have an explanation for this behavior or is this a bug?

2
  • 1
    The <RootNode> has a non-empty XML namespace - so you have to define and use it - I don't understand what your question really is ..... Commented Jan 8, 2013 at 17:33
  • My question is not clear, both /RootNode and /dft:RootNode return the RootNode, so the prefix is not required. Commented Jan 8, 2013 at 17:47

1 Answer 1

4

It is not clear what your problem is. With the XML sample you have posted the RootNode element and the otherNode element are in the namespace http://tempuri.org while the NodeWithEmptyNamespace element is in no namespace (as the xmlns="" puts it there). With XPath a path or step NodeWithEmptyNamespace selects elements of that name in no namespace and RootNode would select an element of that name in no namespace too, only in your input there is no such element. So your path /dft:RootNode/NodeWithEmptyNamespace is doing the right thing, it selects the element with local name RootNode in the namespace tied to the dft prefix (i.e. http://tempuri.org) and its child element with local name NodeWithEmptyNamespace in no namespace.

Sign up to request clarification or add additional context in comments.

1 Comment

I made the confusion that an empty default namespace was the same of no namespace declaration, in other words that <a xmlns=""/> was equal to <a>. In the first case (contrary to what I thought) there is no namespace of the node <a> and the second case the namespace is the same of the parent node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.