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?
<RootNode>
has a non-empty XML namespace - so you have to define and use it - I don't understand what your question really is ..... – marc_s