【问题标题】:Import XML into SQL server using OPENXML command with XMLNS使用带有 XMLNS 的 OPENXML 命令将 XML 导入 SQL 服务器
【发布时间】:2017-03-04 17:27:28
【问题描述】:

我有以下代码将 xml 导入 SQL

DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX)

SELECT @XML = XMLData FROM XMLwithOpenXML

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML

SELECT rid, uid
FROM OPENXML(@hDoc, '/PportTimetable/Journey')
WITH 
(
rid [varchar](50) '@rid',
uid [varchar](100) '@uid'
)

EXEC sp_xml_removedocument @hDoc
GO

我可以让代码工作,但只有当它不包含如下所示的 xmlns 信息时,这是为什么呢?

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://www.thalesgroup.com/rtti/XmlTimetable/v8"

XML 标头

<PportTimetable xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" timetableID="20161018020822" xmlns="http://www.thalesgroup.com/rtti/XmlTimetable/v8">
  <Journey rid="201610188012733" uid="P12733" trainId="2J27" ssd="2016-10-18" toc="AW">
</Journey>
</PportTimetable>

【问题讨论】:

    标签: sql sql-server xml


    【解决方案1】:

    您需要指定命名空间作为 sp_xml_preparedocument 的第三个参数

    DECLARE @XML AS XML='<PportTimetable xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" timetableID="20161018020822" xmlns="http://www.thalesgroup.com/rtti/XmlTimetable/v8">
      <Journey rid="201610188012733" uid="P12733" trainId="2J27" ssd="2016-10-18" toc="AW">
    </Journey>
    </PportTimetable>';
    DECLARE @hDoc AS INT, @SQL NVARCHAR (MAX);
    
    EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML,'<PportTimetable xmlns:b="http://www.thalesgroup.com/rtti/XmlTimetable/v8"/>';
    
    SELECT rid, uid
    FROM OPENXML(@hDoc, '/b:PportTimetable/b:Journey')
    WITH 
    (
    rid [varchar](50) '@rid',
    uid [varchar](100) '@uid'
    )
    
    EXEC sp_xml_removedocument @hDoc
    GO
    

    【讨论】:

      【解决方案2】:

      我建议完全跳过 OPENXML 的内容并使用 SQL Server 中内置的原生 XQuery 支持:

      declare @input XML = '<PportTimetable xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" timetableID="20161018020822" xmlns="http://www.thalesgroup.com/rtti/XmlTimetable/v8">
          <Journey rid="201610188012733" uid="P12733" trainId="2J27" ssd="2016-10-18" toc="AW">
          </Journey>
      </PportTimetable>'
      
      -- define your XML namespaces - here, there's only a single "default" namespace    
      ;WITH XMLNAMESPACES(DEFAULT 'http://www.thalesgroup.com/rtti/XmlTimetable/v8')
      SELECT 
          RID = XC.value('@rid', 'varchar(50)'),
          UID = XC.value('@uid', 'varchar(20)'),
          TrainId = XC.value('@trainId', 'varchar(25)'),
          SSD = XC.value('@ssd', 'varchar(25)'),
          TOC = XC.value('@toc', 'varchar(20)')
      FROM 
          @input.nodes('/PportTimetable/Journey') AS XT(XC)
      

      使用 XQuery .nodes() 函数将您的 XML “分解”为 XML 片段的“内联”表(在本例中,每个 &lt;Journey&gt; 节点一个),然后使用 .value() 函数获取这些 XML 片段中的单个元素和属性一一对应。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-29
        • 2020-06-19
        • 1970-01-01
        • 1970-01-01
        • 2013-10-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多