zoukankan      html  css  js  c++  java
  • OPENXML with xmlns:dt

    0 vote down star

    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
    摘自:http://stackoverflow.com/questions/1440848/openxml-with-xmlnsdt
  • 相关阅读:
    poj3107 Godfather
    poj1655 Balancing Act
    bzoj2073 PRZ
    bzoj1688 疾病管理
    BZOJ——1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
    BZOJ——1606: [Usaco2008 Dec]Hay For Sale 购买干草
    BZOJ——1649: [Usaco2006 Dec]Cow Roller Coaster
    BZOJ——2096: [Poi2010]Pilots
    洛谷—— P1785 漂亮的绝杀
    NOIP 2012 提高组 DAY1 T2 国王游戏
  • 原文地址:https://www.cnblogs.com/wuming/p/1609140.html
Copyright © 2011-2022 走看看