简体   繁体   English

Oracle XMLTYPE 提取根 prolog 值

[英]Oracle XMLTYPE extract root prolog value

SELECT * FROM v$version;
*Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
PL/SQL Release 12.1.0.2.0 - Production
"CORE   12.1.0.2.0  Production"
TNS for Linux: Version 12.1.0.2.0 - Production
NLSRTL Version 12.1.0.2.0 - Production*

I have sample Query with XML like below:我有 XML 的示例查询,如下所示:

with t(xml) as 
(
select xmltype(
'<SSO_XML
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
TimeStamp="2020-08-05T21:57:23Z" 
Target="Production" 
Version="1.0" 
TransactionIdentifier="PLAN_A" 
SequenceNmbr="123456"
    xmlns="http://www.w3.org/2001/XMLSchema">
    <PlanCode PlanCodeCode="CHOICE">
        <S_DAYS FARE="10" Start="2020-08-07" End="2020-10-30" Mon="true" Tue="true" Weds="true" Thur="true" Fri="true" Sat="true" Sun="true">
            <STUDENT>
                <DIVISION ORIGINAL="150.05" Code="Flat" S_CODE="1" />
                <DIVISION ORIGINAL="150.05" Code="Flat" S_CODE="2" />
            </STUDENT>
        </S_DAYS>
    </PlanCode>
</SSO_XML>') 
 from dual
 )

select h.PlanCodeCode
,b.Original
,b.code
,b.s_code
 from   t
    cross join
    xmltable(xmlnamespaces(default 'http://www.w3.org/2001/XMLSchema'),
             '/SSO_XML'
             passing t.xml
             columns PlanCodeCode varchar2(100)  path './PlanCode/@PlanCodeCode',
                     attributes xmltype path './PlanCode'
            ) h
    cross join xmltable(xmlnamespaces(default 'http://www.w3.org/2001/XMLSchema'),
             'PlanCode/S_DAYS/STUDENT/DIVISION'
             passing h.attributes
             columns 
                    ORIGINAL number path  '@ORIGINAL',
                    Code            varchar2(100) path '@Code',
                    S_CODE  number path '@S_CODE'
            ) b;

Output: Output:

在此处输入图像描述

I am trying to fetch the values below within the query or a new query: Is there any way to do that?我正在尝试在查询或新查询中获取以下值:有什么办法吗? Any help / direction appreciated.任何帮助/方向表示赞赏。

在此处输入图像描述

You just have to complete your first XMLTABLE query (h) in this way:您只需以这种方式完成您的第一个 XMLTABLE 查询 (h):

target  varchar2(100) path '/SSO_XML/@Target',
transactionId  varchar2(100) path '/SSO_XML/@TransactionIdentifier'

You are trying to include two attributes, which are accessed with a @ prefix;您正在尝试包含两个属性,它们使用@前缀访问; so your query can be modified to:因此您的查询可以修改为:

select h.PlanCodeCode
,h.target
,h.transactionIdentifier
,b.Original
,b.code
,b.s_code
 from   t
    cross join
    xmltable(xmlnamespaces(default 'http://www.w3.org/2001/XMLSchema'),
             '/SSO_XML'
             passing t.xml
             columns PlanCodeCode varchar2(100)  path './PlanCode/@PlanCodeCode',
                     target varchar2(100) path '@Target',
                     transactionIdentifier varchar2(100) path '@TransactionIdentifier',
                     attributes xmltype path './PlanCode'
            ) h
...

db<>fiddle db<>小提琴

In this case you could also do this in a single XMLTable call, which is shorter but possibly harder to read and maintain:在这种情况下,您也可以在单个 XMLTable 调用中执行此操作,该调用更短,但可能更难阅读和维护:

select b.PlanCodeCode
,b.target
,b.transactionIdentifier
,b.Original
,b.code
,b.s_code
 from   t
    cross join
    xmltable(xmlnamespaces(default 'http://www.w3.org/2001/XMLSchema'),
             '/SSO_XML/PlanCode/S_DAYS/STUDENT/DIVISION'
             passing t.xml
             columns PlanCodeCode varchar2(100)  path './../../../@PlanCodeCode',
                     target varchar2(100) path './../../../../@Target',
                     transactionIdentifier varchar2(100) path './../../../../@TransactionIdentifier',
                     ORIGINAL number path  '@ORIGINAL',
                     Code varchar2(100) path '@Code',
                     S_CODE  number path '@S_CODE'
            ) b;

db<>fiddle db<>小提琴

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

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