提取XML值

时间:2019-03-21 15:17:01

标签: sql xml oracle plsql

这是我的xml消息:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:wsa="http://www.w3.org/2005/08/addressing">
 <env:Header>
 <wsa:Action>http://xmlns.oracle.com/apps/hcm/trees/organizationTreeService//OrganizationTreeService/addValueOrganizationTreeNodeResponse</wsa:Action>
 <wsa:MessageID>urn:uuid:d8f54d7e-8319-492a-9e0f-ec48ba2e840c</wsa:MessageID></env:Header><env:Body>
 <ns0:addValueOrganizationTreeNodeResponse xmlns:ns0="http://xmlns.oracle.com/apps/hcm/trees/organizationTreeService/types/">
 <ns2:treeNodeId xmlns:ns2="http://xmlns.oracle.com/apps/hcm/trees/organizationTreeService/types/" xmlns:ns1="http://xmlns.oracle.com/apps/hcm/trees/organizationTreeService/" xmlns:tns="http://xmlns.oracle.com/adf/svc/errors/" xmlns:ns0="http://xmlns.oracle.com/adf/svc/types/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns0:StringResult">
 <ns0:Value>80CF98290891BB06E053C8B0BB0A2848</ns0:Value></ns2:treeNodeId></ns0:addValueOrganizationTreeNodeResponse>
 </env:Body></env:Envelope>

我需要从此xml消息中获取值80CF98290891BB06E053C8B0BB0A2848

我正在尝试将此代码放入xmltable中,然后放入mt自定义表中

select  y.JobFamilyId 
  INTO L_PARENTTRENODEID  

 from (select xmltype (P_RESPONSE) xml from dual) t, 
     xmltable(
  xmlnamespaces ( 'http://xmlns.oracle.com/apps/hcm/trees/organizationTreeService/' as "ns0" ),
  '//ns1:value'
  passing t.xml
  columns
 value varchar2(500) path '/*:value'
 ) x 

1 个答案:

答案 0 :(得分:1)

您为ns0命名空间指定的路径从头到尾都缺少/types-尽管您为ns0使用了错误的URL,而该URL实际上对Value节点;您的XPath具有ns1:value而不是ns0:Value,但需要通配或具有完整路径;并且columns子句可以简化为'.'

select x.value
into l_parenttrenodeid  
from xmltable(
  xmlnamespaces('http://xmlns.oracle.com/adf/svc/types/' as "ns0"),
  '//ns0:Value'
  passing xmltype(p_response)
  columns value varchar2(500) path '.'
) x;

或通配符(无需NS声明)

select x.value
into l_parenttrenodeid  
from xmltable(
  '//*:Value'
  passing xmltype(p_response)
  columns value varchar2(500) path '.'
) x;

或具有指向该节点的完整XPath(在此处拆分,因此一部分进入columns子句,以增加乐趣并减少滚动),并声明了该路径中的所有名称空间-作为至少一个URL令人困惑用于多个名称,至少一个名称用于多个URL。幸运的是,名称不必匹配,只需将其解析为正确的路径即可:

select x.value
into l_parenttrenodeid  
from xmltable(
  xmlnamespaces(
    'http://schemas.xmlsoap.org/soap/envelope/' as "env",
    'http://xmlns.oracle.com/apps/hcm/trees/organizationTreeService/types/' as "ns1",
    'http://xmlns.oracle.com/adf/svc/types/' as "ns2"
  ),
  '/env:Envelope/env:Body/ns1:addValueOrganizationTreeNodeResponse/ns1:treeNodeId'
  passing xmltype(p_response)
  columns value varchar2(500) path 'ns2:Value'
) x;

您也可以使用XMLQuery()而不是XMLTable()进行呼叫,因为您只期望得到一个结果:

select xmlquery(
  'declare namespace env = "http://schemas.xmlsoap.org/soap/envelope/"; (::)
    declare namespace ns1 = "http://xmlns.oracle.com/apps/hcm/trees/organizationTreeService/types/"; (::)
    declare namespace ns2 = "http://xmlns.oracle.com/adf/svc/types/"; (::)
    /env:Envelope/env:Body/ns1:addValueOrganizationTreeNodeResponse/ns1:treeNodeId/ns2:Value/text()'
  passing xmltype(p_response)
  returning content
).getstringval()
into l_parenttrenodeid
from dual;