使用Open XML来提取CDATA

时间:2015-02-18 15:11:20

标签: sql-server sql-server-openxml

我有xml,其中包含嵌入CDATA的数据。我想在不同的领域提取信息。但是无法做到。

<Item_Response Format="text/xml">   
<![CDATA[ <Item sequence="1" type="item" itemId="999999" 
itemVersion="2012-04-07T13:43:27">
<response><bubbleinput answered="y" input_id="bubbleinput1">
<bubble id="bubble1"/>
</bubbleinput></response></Item> ]]>
</Item_Response> 

1 个答案:

答案 0 :(得分:1)

您可以使用XPath提取CDATA值。然后在提取的值上使用openxml。

declare @xml xml = '<Item_Response Format="text/xml"><![CDATA[ <Item sequence="1" type="item" itemId="999999" itemVersion="2012-04-07T13:43:27"><response><bubbleinput answered="y" input_id="bubbleinput1"><bubble id="bubble1"/></bubbleinput></response></Item> ]]></Item_Response>'

-- openxml
declare
    @idoc int,
    @qxml xml = cast(@xml.value('(/Item_Response)[1]', 'nvarchar(max)') as xml)

exec sp_xml_preparedocument @idoc output, @qxml

select
    *
from
    openxml(@idoc, '/Item', 0) with (
        sequence int '@sequence',
        bubbleinput nvarchar(1) './response/bubbleinput/@answered'
    ) as XMLData

exec sp_xml_removedocument @idoc