简体   繁体   English

使用Oracle SQL读取XML命名空间

[英]Reading XML Namespace using Oracle SQL

My XML Looks like below 我的XML如下所示

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>  
<wfm:Statement xmlns:wfm="http://example.org/sample/xsd/sampleStatement/2013/05" xmlns:wfmMerchant="http://www.eds.com/sample/xsd/wfmMerchant/2012/03"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  <wfm:StatementParameters>  
    <wfmMerchant:HierarchyCd>012-12-002-107-050</wfmMerchant:HierarchyCd>   
  </wfm:StatementParameters>  
  <StatementAmount>27.140</StatementAmount>  
</wfm:Statement>

I am trying to get the value of StatementAmount tag using Oracle query like below 我正在尝试使用如下所示的Oracle查询来获取StatementAmount标记的值

select MS.MERCHANT,MS.CHAIN_HIERARCHY_CD,MS.CYCLE_DATE, X.StatementAmount
FROM CHAIN_STATMNT_HIST_XML MS  
CROSS JOIN XMLTABLE(XMLNAMESPACES('http://example.org/sample/xsd/sampleStatement/2013/05' AS "wfm", 'http://www.eds.com/sample/xsd/wfmMerchant/2012/03' as wfmmerchant
     default 'http://www.w3.org/2001/XMLSchema-instance')
     ,'/wfm:Statement/StatementAmount' passing xmltype(MS.XML_REPORT) 
     columns StatementAmount varchar(18) path '.')X

But, I am always getting NULL . 但是,我总是得到NULL I can able to successfully retrieve Hierarchy value from the XML which has namespace. 我可以从具有名称空间的XML成功检索层次结构值。 But StatementAmount tag doesn't have any namespace and I have trouble retrieving it. 但是StatementAmount标记没有任何名称空间,我在检索它时遇到了麻烦。

Can someone help with this issue ? 有人可以解决这个问题吗?

Your default namespace declaration seems to be causing the problem; 您的默认名称空间声明似乎是造成此问题的原因。 without that (and ignoring wfmMerchant ): 没有那个(并忽略wfmMerchant ):

-- CTE for sample data
with CHAIN_STATMNT_HIST_XML (merchant, chain_hierarchy_cd, cycle_date, XML_REPORT) as (
  select 1, 2, sysdate, '<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<wfm:Statement xmlns:wfm="http://example.org/sample/xsd/sampleStatement/2013/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<wfm:StatementParameters>
<!-- excluding this as namespace not provided -->
<!-- <wfmMerchant:HierarchyCd>012-12-002-107-050</wfmMerchant:HierarchyCd> -->
</wfm:StatementParameters>
<StatementAmount>27.140</StatementAmount>
</wfm:Statement>' from dual
)
-- actual query
select MS.MERCHANT,MS.CHAIN_HIERARCHY_CD,MS.CYCLE_DATE, X.StatementAmount
FROM CHAIN_STATMNT_HIST_XML MS  
CROSS JOIN XMLTABLE(
  XMLNAMESPACES('http://example.org/sample/xsd/sampleStatement/2013/05' AS "wfm"),
  '/wfm:Statement/StatementAmount' passing xmltype(MS.XML_REPORT)
  columns StatementAmount varchar(18) path '.'
) X
/

  MERCHANT CHAIN_HIERARCHY_CD CYCLE_DATE STATEMENTAMOUNT   
---------- ------------------ ---------- ------------------
         1                  2 2018-09-04 27.140            

I'm not sure why you would use varchar2(18) as the datatype rather than number ; 我不确定为什么要使用varchar2(18)作为数据类型,而不是number and if there is only one statement amount per statement you could do: 并且如果每个语句只有一个语句量,则可以执行以下操作:

select MS.MERCHANT,MS.CHAIN_HIERARCHY_CD,MS.CYCLE_DATE, X.StatementAmount
FROM CHAIN_STATMNT_HIST_XML MS  
CROSS JOIN XMLTABLE(
  XMLNAMESPACES('http://example.org/sample/xsd/sampleStatement/2013/05' AS "wfm"),
  '/wfm:Statement' passing xmltype(MS.XML_REPORT)
  columns StatementAmount number path 'StatementAmount'
) X

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM