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:
- How to start querying the Xml starting from the
<PlaceOrderResponse>
node and not the root to get the correct node(s) - How to parse the values from inside the nodes I am looking at
(StatusCode, ReplyText, CustomerOrder)
. - 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.