带有 xmlns:dt 的 OPENXML

发布于 2024-08-05 06:03:42 字数 2072 浏览 5 评论 0原文

使用 OPENXML 获取 MSSQL 2005 中的 dt 元素。 如何获取 xml 中的 xmlns:dt 元素?例如,获取包含两行的结果集,其中列出了产品 ID 和国家/地区代码。

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

这是我知道的使用 xmlEdgeTable 的一个解决方案,但我正在寻找更好的解决方案。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里人 2024-08-12 06:03:42

您需要使用 OPENXML 来执行此操作是否有特殊原因?您可以使用 2005 年的 XQUERY 轻松获取信息,如下所示:

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)

较新的 XQUERY 功能是解决您的问题的更好选择。

编辑:
与 OPENXML 相同的解决方案是:

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

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
却一份温柔 2024-08-12 06:03:42

对于小型数据集,XQuery 和 OPENXML 之间没有太大区别

以下是解析 6.5 MB xml 文件以获得 27,615 行的结果:

  1. OPENXML with dt:2 秒
  2. OPENXML with #xmlEdgeTable:6 分 38 秒
  3. XQuery:24 分 54 秒

For a small dataset, there is no big difference between XQuery and OPENXML

Here are results to parse a 6.5 MB xml file to get 27,615 rows:

  1. OPENXML with dt: 2 seconds
  2. OPENXML with #xmlEdgeTable: 6 minutes 38 seconds
  3. XQuery: 24 minutes 54 seconds
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文