从 XML 到 Oracle PL/SQL 环境中的路径列表

From XML to list of paths in Oracle PL/SQL environment(从 XML 到 Oracle PL/SQL 环境中的路径列表)
本文介绍了从 XML 到 Oracle PL/SQL 环境中的路径列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请假设您有一个 XML 文件(例如,存储在还有一个 CLOB 列的 Oracle 表中):

Please suppose you have a XML file (stored, for example, in an Oracle table which has also a CLOB column):

<ALFA>
  <BETA>0123</BETA>
  <GAMMA>2345</GAMMA>
  <DELTA>
     <EPSILON>3</EPSILON>
  </DELTA>
</ALFA>

如何在输出中生成所有可能路径的列表?

How can I produce, in output, the list of all possible paths?

/ALFA/BETA/text()
/ALFA/GAMMA/text()
/ALFA/DELTA/EPSILON/text()

我的需求如下:我必须从长的 XML 中提取许多信息,并且我必须将 XMLEXTRACT 与所有可能的路径一起使用,所以我想知道是否可以自动dbms_output.put_line"它们方式.

My need is the following: I have to EXTRACT many information from a long XML and I have to use XMLEXTRACT with all possible paths, so I would like to know if is it possible to "dbms_output.put_line" them in an automatic way.

我需要一个独立于标签名称的解决方案.

I need a solution which is independent from the name of the tags.

请假设 XML 格式良好.

Please suppose that the XML is well-formed.

预先感谢您的帮助.

  • 第二种情况:

如果尚未安装 Oracle Java Extension,我该如何继续,并且收到以下错误?

How can I proceed if Oracle Java Extension has not been installed, and I receive the following error?

ORA-19112: error raised during evaluation:  
ORA-06550: line 1, column 13:
PLS-00201: identifier 'SYS.DBMS_XQUERYINT' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

请假设我不是 DBA,并且 DBA 不授权安装 Oracle Java Extension.

Please suppose that I am not DBA, and DBA don't authorize Oracle Java Extension installation.

推荐答案

您可以使用 XMLTable 使用 XQuery 生成路径列表.

You can use XMLTable to produce list of paths with XQuery.

例如

(SQLFiddle)

with params as (
  select 
    xmltype('
      <ALFA>
        <BETA>0123</BETA>
        <GAMMA>2345</GAMMA>
        <DELTA>
           <EPSILON>3</EPSILON>
        </DELTA>
      </ALFA>
    ') p_xml
  from dual  
)    
select
  path_name || '/text()'
from
  XMLTable(
    '
      for $i in $doc/descendant-or-self::*
        return <element_path> {$i/string-join(ancestor-or-self::*/name(.), ''/'')} </element_path>
    '
    passing (select p_xml from params) as "doc"
    columns path_name varchar2(4000) path '//element_path'
  )

但这是一种错误的方式,至少因为它并不有效.

but it's a wrong way at least because it's not effective as it can.

只需使用相同的 XQuery 提取所有值:(SQLFiddle)

Just extract all values with same XQuery: (SQLFiddle)

with params as (
  select 
    xmltype('
      <ALFA>
        <BETA>0123</BETA>
        <GAMMA>2345</GAMMA>
        <DELTA>
           <EPSILON>3</EPSILON>
        </DELTA>
      </ALFA>
    ') p_xml
  from dual  
)    
select
  element_path, element_text
from
  XMLTable(
    '              
      for $i in $doc/descendant-or-self::*
        return <element>
                 <element_path> {$i/string-join(ancestor-or-self::*/name(.), ''/'')} </element_path>
                 <element_content> {$i/text()}</element_content>
               </element>  
    '
    passing (select p_xml from params) as "doc"
    columns 
      element_path   varchar2(4000) path '//element_path',
      element_text   varchar2(4000) path '//element_content'
  )

这篇关于从 XML 到 Oracle PL/SQL 环境中的路径列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

SQL to Generate Periodic Snapshots from Transactions Table(用于从事务表生成定期快照的SQL)
MyBatis support for multiple databases(MyBatis支持多个数据库)
Oracle 12c SQL: Missing column Headers in result(Oracle 12c SQL:结果中缺少列标题)
SQL query to find the number of customers who shopped for 3 consecutive days in month of January 2020(查询2020年1月连续购物3天的客户数量)
How to get top 10 data weekly (This week, Previous week, Last month, 2 months ago, 3 month ago)(如何每周获取前十大数据(本周、前一周、上个月、2个月前、3个月前))
Select the latest record for an Id per day - Oracle pl sql(选择每天ID的最新记录-Oracle pl SQL)