如何在Oracle中通过xmltype解析XML
问题描述:
当我尝试解析XML文档时,我在这里遇到了XPath属性的小问题。 这是我的例子:如何在Oracle中通过xmltype解析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
放在PATH或XQUery字符串中,但没有成功。
我认为这是一个小而简单的问题,但我无法找到。 在此先感谢! Filipe
答
您在ns1
名称空间声明中缺少urn:
前缀。您也忽略<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;
感谢亚历克斯。这显然是一个小路径问题。 – milheiros