Как разобрать XML по xmltype в Oracle
У меня есть небольшая проблема со свойством XPath, когда я пытаюсь проанализировать XML-документ. Это мой пример:
DECLARE
px_return XMLTYPE
:= XMLTYPE (
'<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Header xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<h:AxisValues xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:h="urn:/microsoft/multichannelframework/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:/microsoft/multichannelframework/">
<User xmlns="">FSCD</User>
<Solution xmlns="">Multicare</Solution>
<ApplicationalUser xmlns=""/>
<ApplicationalUserSystem xmlns=""/>
<SystemUser xmlns=""/>
<SystemUserSystem xmlns=""/>
<Proxy xmlns="">0</Proxy>
</h:AxisValues>
</SOAP:Header>
<SOAP:Body xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<ns1:maintainMandateResponse xmlns:ns1="urn:enterprise.com/ws/SAP/Finantial/MaintainMandate/V1">
<return>
<messageType>E</messageType>
</return>
</ns1:maintainMandateResponse>
</SOAP:Body>
</soapenv:Envelope>');
lv_msgType VARCHAR2 (20);
BEGIN
SELECT Return.msgType
INTO lv_msgType
FROM XMLTABLE (
xmlnamespaces (
DEFAULT 'enterprise.com/ws/SAP/Finantial/MaintainMandate/V1',
'http://schemas.xmlsoap.org/soap/envelope/' AS "soapenv",
'http://schemas.xmlsoap.org/soap/envelope/' AS "SOAP",
'enterprise.com/ws/SAP/Finantial/MaintainMandate/V1' AS "ns1"),
'//soapenv:Envelope/SOAP:Body/ns1:maintainMandateResponse'
PASSING px_return
COLUMNS msgType VARCHAR2 (1) PATH 'messageType') Return;
DBMS_OUTPUT.put_line ('Message type: ' || lv_msgType);
END;
Я получаю исключение NO_DATA_FOUND, потому что я не могу найти результаты в этом методе анализа.
Я перепробовал много разных стратегий, включая return
в ПУТИ или в строке XQUery, но безуспешно.
Я думаю, что это небольшая и простая проблема, но я не могу найти. Заранее спасибо! Филипе
1 ответ
Вы скучаете по urn:
префикс в вашем ns1
декларация пространства имен. Вы также игнорируете <return>
уровень узла, и у вас есть пространство имен по умолчанию, которое является неправильным, поскольку у вас есть дочерние узлы без какого-либо пространства имен. Итак, вам нужно:
SELECT Return.msgType
INTO lv_msgType
FROM XMLTABLE (
xmlnamespaces (
'http://schemas.xmlsoap.org/soap/envelope/' AS "soapenv",
'http://schemas.xmlsoap.org/soap/envelope/' AS "SOAP",
'urn:enterprise.com/ws/SAP/Finantial/MaintainMandate/V1' AS "ns1"),
'/soapenv:Envelope/SOAP:Body/ns1:maintainMandateResponse'
PASSING px_return
COLUMNS msgType VARCHAR2 (1) PATH 'return/messageType') Return;
Который получает:
PL/SQL procedure successfully completed.
Message type: E
Или вы также можете переместить возврат в XPath, что имеет тот же эффект:
'/soapenv:Envelope/SOAP:Body/ns1:maintainMandateResponse/return'
PASSING px_return
COLUMNS msgType VARCHAR2 (1) PATH 'messageType') Return;