Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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:</p> <pre><code>declare @xmldata xml set @xmldata = '&lt;data xmlns="http://www.aaa.com/master_browse_response" xmlns:dt="http://www.aaa.com/DataTypes"&gt; &lt;products&gt; &lt;product&gt; &lt;product_id&gt;121403&lt;/product_id&gt; &lt;countries&gt; &lt;dt:country&gt;GBR&lt;/dt:country&gt; &lt;dt:country&gt;USA&lt;/dt:country&gt; &lt;/countries&gt; &lt;/product&gt; &lt;/products&gt; &lt;/data&gt;' ;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) </code></pre> <p>The newer XQUERY capabilities are a much better choice for solving your problem.</p> <p>EDIT: The same solution with OPENXML would be:</p> <pre><code>declare @xmldata xml set @xmldata = '&lt;data xmlns="http://www.aaa.com/master_browse_response" xmlns:dt="http://www.aaa.com/DataTypes"&gt; &lt;products&gt; &lt;product&gt; &lt;product_id&gt;121403&lt;/product_id&gt; &lt;countries&gt; &lt;dt:country&gt;GBR&lt;/dt:country&gt; &lt;dt:country&gt;USA&lt;/dt:country&gt; &lt;/countries&gt; &lt;/product&gt; &lt;/products&gt; &lt;/data&gt;' DECLARE @hDoc int, @rootxmlns varchar(100) SET @rootxmlns = '&lt;root xmlns:hm="http://www.aaa.com/master_browse_response" xmlns:dt="http://www.aaa.com/DataTypes"/&gt;' 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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload