Reputation: 55
How do I select the value of "pagekey" from the xml below with SQL on SQL Server? I tried .nodes using namespaces but cannot find the correct syntax.
Thank you,
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header>
<WorkContext xmlns="http://oracle.com/weblogic/soap/workarea/">rO03ZWJsb2dpYy5hcHAuU0JNLVJhcG9yV1MAAADWAAAAI3dlYmxvZ2ljLndvcmthcmVhL0cmluZ1dvcmtDb250ZXh0ABIyMDIwLE4LjE0MjIuNDAAAA==</WorkContext>
</S:Header>
<S:Body>
<ns0:getReportOutputResponse xmlns:ns0="http://report_xml.org">
<return>
<pagekey>i6161140E964FF7A072CD2E3F2BB9C0</pagekey>
<report><?xml version="1.0"?><dataSet xmlns="http://report_xml.org/dataSet/201006"><dataTable><id>C1</id></dataTable></dataSet></report>
</return>
</ns0:getReportOutputResponse>
</S:Body>
</S:Envelope>
Upvotes: 1
Views: 42
Reputation: 754468
Something like this?
DECLARE @data XML = '<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Header>
<WorkContext xmlns="http://oracle.com/weblogic/soap/workarea/">rO03ZWJsb2dpYy5hcHAuU0JNLVJhcG9yV1MAAADWAAAAI3dlYmxvZ2ljLndvcmthcmVhL0cmluZ1dvcmtDb250ZXh0ABIyMDIwLE4LjE0MjIuNDAAAA==</WorkContext>
</S:Header>
<S:Body>
<ns0:getReportOutputResponse xmlns:ns0="http://report_xml.org">
<return>
<pagekey>i6161140E964FF7A072CD2E3F2BB9C0</pagekey>
<report><?xml version="1.0"?><dataSet xmlns="http://report_xml.org/dataSet/201006"><dataTable><id>C1</id></dataTable></dataSet></report>
</return>
</ns0:getReportOutputResponse>
</S:Body>
</S:Envelope>';
-- define the two relevant XML namespaces
WITH XMLNAMESPACES('http://schemas.xmlsoap.org/soap/envelope/' AS soap,
'http://report_xml.org' AS RP)
SELECT
-- get the "pagekey" element from the "body"
XC.value('(RP:getReportOutputResponse/return/pagekey/text())[1]', 'VARCHAR(100)')
FROM
-- get the <s:Body> part as XML fragment
@data.nodes('/soap:Envelope/soap:Body') AS XT(XC)
Upvotes: 1