使用Openxml在SQL Server中解析xml

时间:2017-09-18 09:16:03

标签: sql-server xml tsql openxml

请让我知道为什么我的查询不起作用。 以下是我的XML。 试图在SQL Server中解析它。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<PostEDIDataToEIP xmlns="http://tempuri.org/">
<xmlRequestDetails>
<Applicant>
<ApplyData>
<RefNo>86</RefNo>
<ProfileSnapshot>
<Name>King</Name>
<ResumeHeadline>RESUME HEADLINE.......................</ResumeHeadline>
</ProfileSnapshot>
</ApplyData>
</Applicant>
</xmlRequestDetails>
</PostEDIDataToEIP>
</soap:Body>
</soap:Envelope>

set @xmlRequestDetails='Above xml'
set @xPath='/soap:Envelope/soap:Body/PostEDIDataToEIP/xmlRequestDetails/Applicant/ApplyData' 
Exec sp_xml_preparedocument @XML_Hndl OUTPUT, @xmlRequestDetails
select * into #jobdetails FROM OPENXML(@XML_Hndl,@xPath,2) with (RefNo smallint)
select * from #jobdetails 

set @xPath='/soap:Envelope/soap:Body/PostEDIDataToEIP/xmlRequestDetails/Applicant/ApplyData/ProfileSnapshot' 
select * into #personaldetails FROM OPENXML(@XML_Hndl,@xPath,2) with (Name varchar(100), ResumeHeadline varchar(100))
select * from #personaldetails

收到以下错误。

Msg 6603, Level 16, State 2, Procedure sp_forintegration, Line 31
XML parsing error: Reference to undeclared namespace prefix: 'soap'.
The statement has been terminated.
Msg 208, Level 16, State 0, Procedure sp_forintegration, Line 33
Invalid object name '#jobdetails'.

此xml将插入到Sql server表XML列中。 我觉得我在解析时遇到了一些错误,请让我知道可以做什么,或者如何解析。

感谢/此致 Prasanth Kumar

1 个答案:

答案 0 :(得分:0)

准备XML文档时需要声明名称空间前缀,如下所示:

SET @rootxmlns = '<root xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"/>'
Exec sp_xml_preparedocument @XML_Hndl OUTPUT, @xmlRequestDetails, @rootxmlns

有关更详细的示例,请参阅here