OPENXML with xmlns:dt

Is there a particular reason that you need to use OPENXML to do this? You can easily get the information with a XQUERY in 2005 like this:

declare @xmldata xml    
set @xmldata="<data xmlns="http://www.aaa.com/master_browse_response" xmlns:dt="http://www.aaa.com/DataTypes">
  <products>
    <product>
      <product_id>121403</product_id>
      <countries>
        <dt:country>GBR</dt:country>
        <dt:country>USA</dt:country>
      </countries>
    </product>
  </products>
</data>"

;WITH XMLNAMESPACES 
(
    DEFAULT 'http://www.aaa.com/master_browse_response',
    'http://www.aaa.com/DataTypes' as dt
)
SELECT  x.c.value('(../../product_id)[1]', 'varchar(100)') as product_id,
        x.c.value('(.)[1]', 'varchar(100)') as country
FROM @xmldata.nodes('/data/products/product/countries/dt:country') x(c)

The newer XQUERY capabilities are a much better choice for solving your problem.

EDIT:
The same solution with OPENXML would be:

declare @xmldata xml    
set @xmldata="<data xmlns="http://www.aaa.com/master_browse_response" xmlns:dt="http://www.aaa.com/DataTypes">
  <products>
    <product>
      <product_id>121403</product_id>
      <countries>
        <dt:country>GBR</dt:country>
        <dt:country>USA</dt:country>
      </countries>
    </product>
  </products>
</data>"

DECLARE @hDoc int, @rootxmlns varchar(100)
SET @rootxmlns="<root xmlns:hm="http://www.aaa.com/master_browse_response" xmlns:dt="http://www.aaa.com/DataTypes"/>"
EXEC sp_xml_preparedocument @hDoc OUTPUT, @xmldata, @rootxmlns

SELECT *
FROM OPENXML(@hDoc, '//hm:product/hm:countries/dt:country',2)
        WITH(Country    varchar(100) '.',
             Product_ID varchar(100) '../../hm:product_id')

EXEC sp_xml_removedocument @hDoc

Leave a Comment