1

I have a return of XML that I wish to select using SQL. I don't have a problem selecting from the XML when I specify the index for the element. This is the XML in question.

<root httpStatusCode="200">
    <messages />
    <succesfulResponses>
        <item position="0">
            <response dln="TESTDLN" ServiceVersion="1" hubServiceVersion="1.0.0.0" ProcessingDate="2017-11-20T10:42:20.579Z" hubProcessingDate="2017-11-20T10:41:16.5415151Z" httpStatusCode="200">
                <licence status="FC" validFrom="2017-03-18" validTo="2024-10-31" directiveIndicator="4">
                    <entitlements>
                        <item code="A" validFrom="2006-04-07" validTo="2057-05-20" priorTo="false" type="P">
                            <restrictions />
                        </item>
                        <item code="AM" validFrom="2014-10-21" validTo="2057-05-20" priorTo="false" type="F">
                            <restrictions>
                                <item type="122" info="null" />
                            </restrictions>
                        </item>
                        <item code="B" validFrom="2014-10-21" validTo="2057-05-20" priorTo="false" type="F">
                            <restrictions />
                        </item>
                        <item code="BE" validFrom="2014-11-01" validTo="2057-05-20" priorTo="false" type="P">
                            <restrictions />
                        </item>
                        <item code="F" validFrom="2014-10-21" validTo="2057-05-20" priorTo="false" type="F">
                            <restrictions />
                        </item>
                        <item code="G" validFrom="2014-11-01" validTo="2057-05-20" priorTo="false" type="P">
                            <restrictions />
                        </item>
                        <item code="H" validFrom="2014-11-01" validTo="2057-05-20" priorTo="false" type="P">
                            <restrictions />
                        </item>
                        <item code="K" validFrom="2014-10-21" validTo="2057-05-20" priorTo="false" type="F">
                            <restrictions />
                        </item>
                        <item code="Q" validFrom="2014-10-21" validTo="2057-05-20" priorTo="false" type="F">
                            <restrictions>
                                <item type="122" info="null" />
                            </restrictions>
                        </item>
                    </entitlements>
                    <endorsements />
                </licence>
                <messages />
            </response>
        </item>
    </succesfulResponses>
    <errorResponses />
</root>

The SQL I have written so far is as follows:

select
Rec.value('(@dln)[1]','char(50)'),
Rec.value('(licence/@status)[1]','char(2)'),
pd.value('(entitlements/item/@code)[1]','char(2)')
FROM @xmlData.nodes('//root/succesfulResponses/item/response') as x(Rec)
cross apply @xmlData.nodes('//root/succesfulResponses/item/response/licence') as i(pd)

This returns the obviously First Row code of 'A', however there can be multiple 'entitlements' and I don't ever know how many there could be 3 there could be 9.

I thought a Cross Apply would work but I can't seem to make that work either.

Any thoughts/help.

2 Answers 2

2

Try this - expand the XPath after the CROSS APPLY to include the entitlements/item part:

select
    Rec.value('(@dln)[1]','char(50)'),
    Rec.value('(licence/@status)[1]','char(2)'),
    pd.value('@code', 'char(2)')
FROM
     @xmlData.nodes('//root/succesfulResponses/item/response') as x(Rec)
cross apply 
    @xmlData.nodes('//root/succesfulResponses/item/response/licence/entitlements/item') as i(pd)

Returns:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Top man - I tried something similar but couldn't get it to work.
0

The variant with OPENXML

DECLARE @idoc int, @doc varchar(MAX)

SET @doc='<root httpStatusCode="200">
    <messages />
    <succesfulResponses>
        <item position="0">
            <response dln="TESTDLN" ServiceVersion="1" hubServiceVersion="1.0.0.0" ProcessingDate="2017-11-20T10:42:20.579Z" hubProcessingDate="2017-11-20T10:41:16.5415151Z" httpStatusCode="200">
                <licence status="FC" validFrom="2017-03-18" validTo="2024-10-31" directiveIndicator="4">
                    <entitlements>
                        <item code="A" validFrom="2006-04-07" validTo="2057-05-20" priorTo="false" type="P">
                            <restrictions />
                        </item>
                        <item code="AM" validFrom="2014-10-21" validTo="2057-05-20" priorTo="false" type="F">
                            <restrictions>
                                <item type="122" info="null" />
                            </restrictions>
                        </item>
                        <item code="B" validFrom="2014-10-21" validTo="2057-05-20" priorTo="false" type="F">
                            <restrictions />
                        </item>
                        <item code="BE" validFrom="2014-11-01" validTo="2057-05-20" priorTo="false" type="P">
                            <restrictions />
                        </item>
                        <item code="F" validFrom="2014-10-21" validTo="2057-05-20" priorTo="false" type="F">
                            <restrictions />
                        </item>
                        <item code="G" validFrom="2014-11-01" validTo="2057-05-20" priorTo="false" type="P">
                            <restrictions />
                        </item>
                        <item code="H" validFrom="2014-11-01" validTo="2057-05-20" priorTo="false" type="P">
                            <restrictions />
                        </item>
                        <item code="K" validFrom="2014-10-21" validTo="2057-05-20" priorTo="false" type="F">
                            <restrictions />
                        </item>
                        <item code="Q" validFrom="2014-10-21" validTo="2057-05-20" priorTo="false" type="F">
                            <restrictions>
                                <item type="122" info="null" />
                            </restrictions>
                        </item>
                    </entitlements>
                    <endorsements />
                </licence>
                <messages />
            </response>
        </item>
    </succesfulResponses>
    <errorResponses />
</root>'


EXEC sp_xml_preparedocument @idoc OUTPUT, @doc;

SELECT *  
FROM OPENXML(@idoc,'/root/succesfulResponses/item/response/licence/entitlements/item',2)   
  WITH (
          status        varchar(10)         '../../@status',
          code          varchar(10)         './@code',
          validFrom     date                './@validFrom'
        );

EXEC sp_xml_removedocument @idoc;
GO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.