简体   繁体   English

解析SQL Server 2012中表上的xml数据列

[英]Parse an xml data column on a table in SQL Server 2012

How do I parse an xml column on a table of data in SQL Server 2012 如何解析SQL Server 2012中的数据表上的xml列

Sample data 样本数据

<GetOfferAvailabilityResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p3="http://somewebsite.com/v2.0" xmlns="http://somewebsite.com/v2.0" p3:TransactionID="281234567">
    <p3:RuleResultList xsi:nil="true" />
    <p3:ResultList>
    <p3:ProviderResult p3:ProviderID="01" p3:ResultID="1234" p3:ResultType="NotAvailable" p3:ResultCode="NotAvailable" p3:BrokerID="55" p3:Structure="None">
    <p3:EntityState>None</p3:EntityState>
    <p3:ResultText>No Orders returned</p3:ResultText>
    <p3:ShortDescription>Not Available</p3:ShortDescription>
    <p3:LongDescription>We're sorry, but offers are currently not available for your service address.</p3:LongDescription>
    <p3:ResultAction>ErrorMessage</p3:ResultAction>
    <p3:SourceResultCode xsi:nil="true" />
        </p3:ProviderResult>
    </p3:ResultList>
    </GetOfferAvailabilityResponse>'

I tried: 我试过了:

DECLARE @x xml
SET @x = 



DECLARE @test TABLE (ID INT, XmlRule XML)
Insert into @test VALUES(1,'
<GetOfferAvailabilityResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" ---GetOfferAvailabilityResponse>')                                   

When I use Select @test.query ('\\') I get the entire xml but when I try Select @test.query ('\\GetOfferAvailabilityResponse') I receive an empty result 当我使用Select @test.query ('\\')我会得到整个xml,但是当我尝试Select @test.query ('\\GetOfferAvailabilityResponse')我会收到一个空结果

You can try something like this: 您可以尝试如下操作:

DECLARE @XmlTbl TABLE (ID INT, XMLDATA XML)

INSERT INTO @XmlTbl 
        ( ID, XMLDATA )
VALUES  ( 1, '<GetOfferAvailabilityResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p3="http://somewebsite.com/v2.0" xmlns="http://somewebsite.com/v2.0" p3:TransactionID="281234567">
    <p3:RuleResultList xsi:nil="true" />
    <p3:ResultList>
    <p3:ProviderResult p3:ProviderID="01" p3:ResultID="1234" p3:ResultType="NotAvailable" p3:ResultCode="NotAvailable" p3:BrokerID="55" p3:Structure="None">
    <p3:EntityState>None</p3:EntityState>
    <p3:ResultText>No Orders returned</p3:ResultText>
    <p3:ShortDescription>Not Available</p3:ShortDescription>
    <p3:LongDescription>We''re sorry, but offers are currently not available for your service address.</p3:LongDescription>
    <p3:ResultAction>ErrorMessage</p3:ResultAction>
    <p3:SourceResultCode xsi:nil="true" />
        </p3:ProviderResult>
    </p3:ResultList>
    </GetOfferAvailabilityResponse>')

;WITH XMLNAMESPACES('http://somewebsite.com/v2.0' AS p3, DEFAULT 'http://somewebsite.com/v2.0')
SELECT
ProviderID = XmlData.value('(/GetOfferAvailabilityResponse/p3:ResultList/p3:ProviderResult/@p3:ProviderID)[1]', 'varchar(50)'),
    EntityState = XmlData.value('(/GetOfferAvailabilityResponse/p3:ResultList/p3:ProviderResult/p3:EntityState)[1]', 'varchar(50)'),
    ResultText = XmlData.value('(/GetOfferAvailabilityResponse/p3:ResultList/p3:ProviderResult/p3:ResultText)[1]', 'varchar(50)'),
    ShortDescription = XmlData.value('(/GetOfferAvailabilityResponse/p3:ResultList/p3:ProviderResult/p3:ShortDescription)[1]', 'varchar(250)'),
    LongDescription = XmlData.value('(/GetOfferAvailabilityResponse/p3:ResultList/p3:ProviderResult/p3:LongDescription)[1]', 'varchar(250)'),
    ResultAction = XmlData.value('(/GetOfferAvailabilityResponse/p3:ResultList/p3:ProviderResult/p3:ResultAction)[1]', 'varchar(50)')
FROM 
    @XmlTbl

From a table that contains a column of type XML , select those bits and pieces that you need, taking into account the defined XML namespaces on your XML data 从包含XML类型列的表中,考虑到XML数据上定义的XML名称空间 ,选择所需的点点滴滴

This gives me a result of: 这给了我以下结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM