0

I am very new to parsing values in Xml and was hoping someone with some Xml (XQUERY/XPATH) knowledge would explain to me how to parse certain values from the following Xml using t-sql. I will include the code attempts I have made to show I am not just asking someone to do it for me.

Given the following Soap Xml Response:


    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:aah:remtp:schemas:PlaceOrderResponse:1.00">
       <SOAP-ENV:Body>
          <PlaceOrderResponse xmlns="urn:aah:remtp:schemas:PlaceOrderResponse:1.00">
             <StatusCode>0</StatusCode>
             <TextReply>Successful</TextReply>
             <PlaceOrderReplyMessage>
                <TransmissionId>aac1e254-bc05-4d69-b04d-31d5fead314e</TransmissionId>
                <ReplyOrder>
                   <TrackingId>aac1e254-bc05-4d69-b04d-31d5fead314e</TrackingId>
                   <CustomerAccount>A_TEST</CustomerAccount>
                   <CustomerOrder>aac1e254-bc05-4d69-b04d-31d5fead314e</CustomerOrder>
                   <ReplyGroup>
                      <LineNumber>1</LineNumber>
                      <ReplyItem>
                         <ReplyCode>0</ReplyCode>
                         <ReplyData>10</ReplyData>
                         <ReplyMsg>Successful</ReplyMsg>
                      </ReplyItem>
                   </ReplyGroup>
                   <ReplyGroup>
                      <LineNumber>2</LineNumber>
                      <ReplyItem>
                         <ReplyCode>0</ReplyCode>
                         <ReplyData>15</ReplyData>
                         <ReplyMsg>Successful</ReplyMsg>
                      </ReplyItem>
                   </ReplyGroup>
                </ReplyOrder>
             </PlaceOrderReplyMessage>
          </PlaceOrderResponse>
       </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

And parse the ReplyGroups into a Table variable (Once I have the select sorted, that should be easy - I think).

I have tried the following:


    DECLARE @XmlDocument XML
    DECLARE @StatusCode VARCHAR(MAX)
    DECLARE @TextReply VARCHAR(MAX)
    DECLARE @CustomerOrder VARCHAR(MAX)
    
    SELECT @XmlDocument = '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:aah:remtp:schemas:PlaceOrderResponse:1.00">
       <SOAP-ENV:Body>
          <PlaceOrderResponse xmlns="urn:aah:remtp:schemas:PlaceOrderResponse:1.00">
             <StatusCode>0</StatusCode>
             <TextReply>Successful</TextReply>
             <PlaceOrderReplyMessage>
                <TransmissionId>aac1e254-bc05-4d69-b04d-31d5fead314e</TransmissionId>
                <ReplyOrder>
                   <TrackingId>aac1e254-bc05-4d69-b04d-31d5fead314e</TrackingId>
                   <CustomerAccount>A_TEST</CustomerAccount>
                   <CustomerOrder>aac1e254-bc05-4d69-b04d-31d5fead314e</CustomerOrder>
                   <ReplyGroup>
                      <LineNumber>1</LineNumber>
                      <ReplyItem>
                         <ReplyCode>0</ReplyCode>
                         <ReplyData>10</ReplyData>
                         <ReplyMsg>Successful</ReplyMsg>
                      </ReplyItem>
                   </ReplyGroup>
                   <ReplyGroup>
                      <LineNumber>2</LineNumber>
                      <ReplyItem>
                         <ReplyCode>0</ReplyCode>
                         <ReplyData>15</ReplyData>
                         <ReplyMsg>Successful</ReplyMsg>
                      </ReplyItem>
                   </ReplyGroup>
                </ReplyOrder>
             </PlaceOrderReplyMessage>
          </PlaceOrderResponse>
       </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>'
    
            --  PlaceOrderResponse/StatusCode
    SELECT @StatusCode = CONVERT(VARCHAR(MAX), @XmlDocument.query('/PlaceOrderResponse/StatusCode'))
    
            --  PlaceOrderResponse/TextReply
    SELECT @TextReply = CONVERT(VARCHAR(MAX), @XmlDocument.query('/PlaceOrderResponse/TextReply'))
    
            --  PlaceOrderResponse/PlaceOrderReplyMessage/ReplyOrder/CustomerOrder
    SELECT @CustomerOrder = CONVERT(VARCHAR(MAX), @XmlDocument.query('/PlaceOrderResponse/PlaceOrderReplyMessage/ReplyOrder'))
    
    
    
    --SELECT @StatusCode As StatusCode, @TextReply As TextReply, @CustomerOrder As CustomerOrder
    --INSERT INTO @ResponseLines(LineNumber, ReplyCode, ReplyData, ReplyMessage)
    SELECT @XmlDocument.query('/PlaceOrderResponse/PlaceOrderReplyMessage/ReplyOrder/ReplyGroup')

Unfortunately all of the variables are empty after this and I think it is because I am not starting from the root node in my selectors. Also, if I remove the two soap envelope nodes and any namespaces, the values are populated but with the actual node and not the value inside each node.

I would be grateful if someone could show me:

  1. How to start querying the Xml starting from the <PlaceOrderResponse> node and not the root to get the correct node(s)
  2. How to parse the values from inside the nodes I am looking at (StatusCode, ReplyText, CustomerOrder).
  3. How to select the multiple <ReplyGroup> Items into a table structure. Please note the <LineNumber> element is higher up the Xml tree hierarchy than the <ReplyItem> Associated with it.
  • "...How to select the multiple `` Items into a table structure...". You need to edit your post, edit the XML structure, and clarify what it means. – Yitzhak Khabinsky Sep 24 '20 at 12:44

1 Answers1

1

You need to take care of namespaces. Check it out how to do it below.

SQL

DECLARE @XmlDocument XML
DECLARE @StatusCode VARCHAR(MAX)
DECLARE @TextReply VARCHAR(MAX)
DECLARE @CustomerOrder VARCHAR(MAX)
    
SELECT @XmlDocument = 
N'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:ns1="urn:aah:remtp:schemas:PlaceOrderResponse:1.00">
    <SOAP-ENV:Body>
        <PlaceOrderResponse xmlns="urn:aah:remtp:schemas:PlaceOrderResponse:1.00">
            <StatusCode>0</StatusCode>
            <TextReply>Successful</TextReply>
            <PlaceOrderReplyMessage>
                <TransmissionId>aac1e254-bc05-4d69-b04d-31d5fead314e</TransmissionId>
                <ReplyOrder>
                    <TrackingId>aac1e254-bc05-4d69-b04d-31d5fead314e</TrackingId>
                    <CustomerAccount>A_TEST</CustomerAccount>
                    <CustomerOrder>aac1e254-bc05-4d69-b04d-31d5fead314e</CustomerOrder>
                    <ReplyGroup>
                        <LineNumber>1</LineNumber>
                        <ReplyItem>
                            <ReplyCode>0</ReplyCode>
                            <ReplyData>10</ReplyData>
                            <ReplyMsg>Successful</ReplyMsg>
                        </ReplyItem>
                    </ReplyGroup>
                </ReplyOrder>
            </PlaceOrderReplyMessage>
        </PlaceOrderResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>';

;WITH XMLNAMESPACES('http://schemas.xmlsoap.org/soap/envelope/' AS [SOAP-ENV]
    ,'urn:aah:remtp:schemas:PlaceOrderResponse:1.00' AS ns2)
SELECT @StatusCode = c.value('(ns2:StatusCode/text())[1]','INT') 
    , @TextReply = c.value('(ns2:TextReply/text())[1]','VARCHAR(100)') 
    , @CustomerOrder = c.value('(ns2:PlaceOrderReplyMessage/ns2:ReplyOrder/ns2:CustomerOrder/text())[1]','VARCHAR(100)') 
FROM @XmlDocument.nodes('/SOAP-ENV:Envelope/SOAP-ENV:Body/ns2:PlaceOrderResponse') AS t(c);

-- test
SELECT @StatusCode AS StatusCode
    , @TextReply AS TextReply
    , @CustomerOrder AS CustomerOrder;

Output

+------------+------------+--------------------------------------+
| StatusCode | TextReply  |            CustomerOrder             |
+------------+------------+--------------------------------------+
|          0 | Successful | aac1e254-bc05-4d69-b04d-31d5fead314e |
+------------+------------+--------------------------------------+

SQL #2

;WITH XMLNAMESPACES('http://schemas.xmlsoap.org/soap/envelope/' AS [SOAP-ENV]
    , DEFAULT 'urn:aah:remtp:schemas:PlaceOrderResponse:1.00')
--INSERT INTO @ResponseLines(LineNumber, ReplyCode, ReplyData, ReplyMessage)
SELECT c.value('(LineNumber/text())[1]','INT') 
    , c.value('(ReplyItem/ReplyCode/text())[1]','VARCHAR(100)') 
    , c.value('(ReplyItem/ReplyData/text())[1]','VARCHAR(100)') 
    , c.value('(ReplyItem/ReplyMsg/text())[1]','VARCHAR(100)') 
FROM @XmlDocument.nodes('/SOAP-ENV:Envelope/SOAP-ENV:Body/PlaceOrderResponse/PlaceOrderReplyMessage/ReplyOrder/ReplyGroup') AS t(c);
Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21
  • That's fantastic @Yitzhak Khabinsky I just need to work out how to extend your answer to retrieve the ReplyGroup nodes (unfortunately the question and Xml fragment I posted only shows the one but there can be multiple). But thanks for the very quick answer to my question. – JulianHolling Sep 24 '20 at 12:49
  • I updated the answer with the possible `INSERT` statement. Check it out. – Yitzhak Khabinsky Sep 24 '20 at 12:53
  • @Yitshak - You're amazing ... thank-you ... Hopefully, moving forward I can use these examples in other XMl responses I get - And I am sure there will be many – JulianHolling Sep 24 '20 at 12:59