且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-04-13 22:28:21

您可以使用 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'
  )