简体   繁体   English

如何在oracle sql中解析xml以下

[英]How to parse below xml in oracle sql

<root>
<transactiondata>
<Info>
    <![CDATA[1234ABCD]]>
<Details>   
    <Data Name="Names">
        <FirstName>A</FirstName>
        <LastName>B</LastName>
        <DisplayName null="true"/>
    </Data>
    <Data Name="Names">
        <FirstName>C</FirstName>
        <LastName>D</LastName>
        <MiddleName>MName</MiddleName>
        <DisplayName>D C</DisplayName>
    </Data>
    <Data Name="Address">
        <Country>ABCDEF</Country>
    </Data>
</Details>
</Info>
<Info>
    <![CDATA[345JUHDE]]>
<Details>
    <Data Name="Names">
        <FirstName>AB</FirstName>
        <LastName>BC</LastName>
        <DisplayName>BC</DisplayName>
    </Data>
    <Data Name="Names">
        <FirstName>CD</FirstName>
        <LastName>DF</LastName>
        <MiddleName null="true"/>
    </Data>
    <Data Name="Phone">
        <PhoneNumber>7654333</PhoneNumber>
    </Data>
</Details>
</Info>
</transactiondata>

The above xml was stored as clob in ClobData table,I want to parse this xml data as temporary table to join this temporary table with other different tables. 上面的xml作为clob存储在ClobData表中,我想将这个xml数据解析为临时表,以将该临时表与其他不同的表连接起来。

    CDATA   FirstName   LastName    DisplayName     MiddleName
1234ABCD    A           B           No Value        null
1234ABCD    C           D           D C             MName
345JUHDE    AB          BC          null            No Value

Can some one please guide me how to parse clob into above format. 有人可以指导我如何解析clob到上面的格式。 I tried TABLE(xmlsequence(extract(xmltype(clobdata.data_cache), '//Info/Data[@Name="Names"]'))) tempTable but i am not able to access cdata. 我试过TABLE(xmlsequence(extract(xmltype(clobdata.data_cache), '//Info/Data[@Name="Names"]'))) tempTable但我无法访问cdata。

You can use the XMLTable function : 您可以使用XMLTable函数

XMLTable maps the result of an XQuery evaluation into relational rows and columns. XMLTable将XQuery评估的结果映射到关系行和列。 You can query the result returned by the function as a virtual relational table using SQL. 您可以使用SQL将函数返回的结果作为虚拟关系表进行查询。

In this case as you have multiple name nodes under each details section, you can either use two levels of XMLTable; 在这种情况下,由于每个详细信息部分下都有多个名称节点,因此您可以使用两个级别的XMLTable; the first gets the CDATA text and the details, the second expands the details to get the name information: 第一个获取CDATA文本和详细信息,第二个扩展细节以获取名称信息:

select x1.cdata, x2.firstname, x2.middlename, x2.lastname, x2.displayname
from clobdata c
cross join xmltable (
  '/root/transactiondata/Info'
  passing xmltype(c.data_cache)
  columns cdata varchar2(10) path './text()',
    details xmltype path 'Details'
) x1
cross join xmltable (
  'Details/Data[@Name="Names"]'
  passing x1.details
  columns firstname varchar2(10) path 'FirstName',
    middlename varchar2(10) path 'MiddleName',
    lastname varchar2(10) path 'LastName',
    displayname varchar2(10) path 'DisplayName'
) x2;

CDATA      FIRSTNAME  MIDDLENAME LASTNAME   DISPLAYNAM
---------- ---------- ---------- ---------- ----------
1234ABCD   A                     B                    
1234ABCD   C          MName      D          D C       
345JUHDE   AB                    BC         BC        
345JUHDE   CD                    DF                   

or go straight to the names and then backtrack to the CDATA: 或直接转到名称,然后回溯到CDATA:

select x.cdata, x.firstname, x.middlename, x.lastname, x.displayname
from clobdata c
cross join xmltable (
  '/root/transactiondata/Info/Details/Data[@Name="Names"]'
  passing xmltype(c.data_cache)
  columns cdata varchar2(10) path './../../text()',
    firstname varchar2(10) path 'FirstName',
    middlename varchar2(10) path 'MiddleName',
    lastname varchar2(10) path 'LastName',
    displayname varchar2(10) path 'DisplayName'
) x;

CDATA      FIRSTNAME  MIDDLENAME LASTNAME   DISPLAYNAM
---------- ---------- ---------- ---------- ----------
1234ABCD   A                     B                    
1234ABCD   C          MName      D          D C       
345JUHDE   AB                    BC         BC        
345JUHDE   CD                    DF                   

Change the sizes of the columns arguments to match the actual lengths of the names you expect to see. 更改columns参数的大小以匹配您希望看到的名称的实际长度。

You can then join x (or if you prefer x1 and/or x2 ) - give the XMLTable's more meaningful aliases though! 然后你可以加入x (或者你更喜欢x1和/或x2 ) - 尽管给XMLTable更有意义的别名! - to other tables as needed. - 根据需要到其他表。

Read more about using XQuery . 阅读有关使用XQuery的更多信息

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

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