我无法从SQL Server 2012中的下面显示的XML中获取值。请帮助
<soapenv:Envelope xmlns:soapenv="http://schemas.abc.org/soap/envelope/" xmlns:wsap="http://schemas.abc.com/abcd">
<soapenv:Header xmlns:wsap="http://schemas.abc.com/abcd" xmlns:soapenv="http://schemas.example.org/soap/envelope/">
<header xmlns:wsap="http://schemas.abc.com/abcd" xmlns:soapenv="http://schemas.example.org/soap/envelope/" xmlns="http://schemas.abc.com/General/1.0/">
<msg-id>0673c07b-2b67-11e7-f959-0b3aae0b96ad</msg-id>
</header>
</soapenv:Header>
<soapenv:Body>
<GetInvoice_DataResponse xmlns:soapenv="http://schemas.example.org/soap/abcd/" xmlns="http://schemas.abc.com/abcd" preserveSpace="no" qAccess="0">
<tuple xmlns="http://schemas.abc.com/abcd">
<old>
<S_ORDER xmlns="http://schemas.abc.com/abcd">
<BILLING_TYPE>Paid</BILLING_TYPE>
<CREATED>2017-11-13T10:40:28.0</CREATED>
<CREATED_BY>1-455454asdas</CREATED_BY>
<LAST_UPD>2017-11-13T10:40:29.0</LAST_UPD>
<LAST_UPD_BY>1-dsafasc</LAST_UPD_BY>
<INVC_DT>2017-11-13T00:00:00.0</INVC_DT>
<INVC_NUM>dasda123</INVC_NUM>
<STATUS_CD>New</STATUS_CD>
<X_CANCELLATION_REASON xmlns:xsi="http://www.example.org/2001/exap-example" null="true" xsi:nil="true" />
<GROSS_AMT>1768</GROSS_AMT>
<ITEM_DTL_AMT>1381.25</ITEM_DTL_AMT>
<ACCNT_ID>1-sad</ACCNT_ID>
<ORDER_ID>1-fasdf</ORDER_ID>
<GROSS_AMT>1768</GROSS_AMT>
<DIV_ID>1-fas</DIV_ID>
<ORDER_NUM>Osasaasf</ORDER_NUM>
<PART>264731200101</PART>
<ORDER_DT>20171113</ORDER_DT>
<INVOICE_DT>20171113</INVOICE_DT>
<ORDER_TYPE>ads fd</ORDER_TYPE>
<QTY>1</QTY>
<LINE_NUM>1</LINE_NUM>
<INVC_STATUS>New</INVC_STATUS>
<LINE_ITEM_STATUS>Invoiced</LINE_ITEM_STATUS>
<BU>CVBU</BU>
</S_ORDER>
</old>
</tuple>
<tuple xmlns="http://schemas.abc.com/abcd">
<old>
<S_ORDER xmlns="http://schemas.abc.com/abcd">
<BILLING_TYPE>Paid</BILLING_TYPE>
<CREATED>2017-11-13T10:40:28.0</CREATED>
<CREATED_BY>1-das</CREATED_BY>
<LAST_UPD>2017-11-13T10:40:29.0</LAST_UPD>
<LAST_UPD_BY>1-safdfa</LAST_UPD_BY>
<INVC_DT>2017-11-13T00:00:00.0</INVC_DT>
<INVC_NUM>afsfas415415</INVC_NUM>
<STATUS_CD>New</STATUS_CD>
<X_CANCELLATION_REASON xmlns:xsi="http://www.example.org/2001/exap-example" null="true" xsi:nil="true" />
<GROSS_AMT>1768</GROSS_AMT>
<ITEM_DTL_AMT>0</ITEM_DTL_AMT>
<ACCNT_ID>1-fasf</ACCNT_ID>
<ORDER_ID>1-saf</ORDER_ID>
<GROSS_AMT>1768</GROSS_AMT>
<DIV_ID>1-1IJGRE5</DIV_ID>
<ORDER_NUM>fasf-sf-s-fd-gd</ORDER_NUM>
<PART>0000079641</PART>
<ORDER_DT>20171113</ORDER_DT>
<INVOICE_DT>20171113</INVOICE_DT>
<ORDER_TYPE>fasdf d</ORDER_TYPE>
<QTY>1</QTY>
<LINE_NUM>2</LINE_NUM>
<INVC_STATUS>New</INVC_STATUS>
<LINE_ITEM_STATUS>Invoiced</LINE_ITEM_STATUS>
<BU>CVBU</BU>
</S_ORDER>
</old>
</tuple>
</GetInvoice_DataResponse>
</soapenv:Body>
</soapenv:Envelope>
答案 0 :(得分:1)
如果您的XML位于@Input
类XML
变量中,则可以尝试以下操作:
-- define your XML namespaces used in the XML document
;WITH XMLNAMESPACES ('http://schemas.abc.org/soap/envelope/' AS soapenv,
'http://schemas.abc.com/abcd' AS inv)
SELECT
-- "reach into" the XML fragment representing the "<S_ORDER>" element
-- and get the individual subelements from it - using the *default*
-- XML namespace for those subelements
BillingType = xc.value('(inv:BILLING_TYPE)[1]', 'varchar(50)'),
Created = xc.value('(inv:CREATED)[1]', 'varchar(50)')
FROM
-- define the path down to the "<S_ORDER>" elements, and fetch
-- an "in-memory" table of each of those elements as a XML fragment
@input.nodes('/soapenv:Envelope/soapenv:Body/inv:GetInvoice_DataResponse/inv:tuple/inv:old/inv:S_ORDER') AS XT(XC)
重点是:您需要在XQuery中尊重并包含 XML命名空间!