Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 390651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:59:07+00:00 2026-05-12T15:59:07+00:00

Use OPENXML to get dt element in MSSQL 2005. How can I get xmlns:dt

  • 0

Use OPENXML to get dt element in MSSQL 2005.
How can I get xmlns:dt element in xml? For example, get a result set of two rows that list product id and country code.

121403 GBR

121403 USA

declare @xmldata xml
    set @xmldata = 
    '<?xml version="1.0"?>
    <data xmlns="http://www.aaa.com/master_browse_response" xmlns:dt="http://www.aaa.com/DataTypes">
      <products>
        <product>
       <product_id><![CDATA[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"/>'

     EXEC sp_xml_preparedocument @hDoc OUTPUT, @xmldata, @rootxmlns  

     SELECT *
     FROM OPENXML(@hDoc, '//hm:product',2)
     WITH ([hm:product_id] int , [hm:countries] varchar(100))

     --clean up 
     EXEC sp_xml_removedocument @hDoc

Here is one solution that I know by using xmlEdgeTable, but I am looking for a better solution.

DECLARE @hDoc int, @rootxmlns varchar(100)
SET @rootxmlns = '<root xmlns:hm="http://www.aaa.com/master_browse_response"/>'

EXEC sp_xml_preparedocument @hDoc OUTPUT, @xmldata, @rootxmlns  

CREATE TABLE #xmlEdgeTable
( 
    id int, 
    parentid int,
    localname varchar(20), 
    [text] varchar(20)
)

INSERT INTO #xmlEdgeTable
SELECT id, parentid,localname, cast([text] as varchar(20)) 
FROM OPENXML(@hDoc, '//hm:product',2)

SELECT t6.text, t2.text FROM #xmlEdgeTable AS t1 INNER JOIN 
    #xmlEdgeTable AS t2 ON t1.id = t2.parentid INNER JOIN 
    #xmlEdgeTable AS t3 ON t3.id = t1.parentid INNER JOIN 
    #xmlEdgeTable AS t4 ON t4.id = t3.parentid INNER JOIN 
    #xmlEdgeTable AS t5 ON t4.id = t5.parentid INNER JOIN
    #xmlEdgeTable AS t6 ON t5.id = t6.parentid 
WHERE t1.localname = 'country' and t5.localname ='product_id'

--clean up 
EXEC sp_xml_removedocument @hDoc
DROP TABLE #xmlEdgeTable
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:59:07+00:00Added an answer on May 12, 2026 at 3:59 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Made a much simpler example, hopefully someone can follow this and help me Here
I'm trying to use a docx read in via the OpenXML SDK as template
USE [Fk_Test2] GO /****** Object: Table [dbo].[Owners] Script Date: 08/20/2010 16:52:44 ******/ SET ANSI_NULLS
use case: 5 - 30 users simultaniously on a chat. is it a good
Use of rails 3 recaptcha. In view _form.html.erb insert <%= recaptcha_tags %> But when
I use the following to visualize the range of means when averaging n coin
I use SQL Server 2008 I use a CHECK CONSTRAINT on multiple columns in
I use restructuredText, and I like what smartypants does for Markdown. Is there a
I have an expensive (time-consuming) external request to another web service I need to
As part of an investigation into enterprise level server side document generation I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.