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

时间:2014-04-30 18:32:46

标签: sql sql-server xml

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

示例数据

<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>'

我试过了:

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>')                                   

当我使用Select @test.query ('\')时,我会获得整个xml但是当我尝试Select @test.query ('\GetOfferAvailabilityResponse')时,我会收到一个空结果

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

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

从包含类型为XML的列的表中,选择您需要的那些部分,并考虑XML数据上定义的 XML命名空间

这给了我一个结果:

enter image description here