簡體   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*

我有 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:

在此處輸入圖像描述

我正在嘗試在查詢或新查詢中獲取以下值:有什么辦法嗎? 任何幫助/方向表示贊賞。

在此處輸入圖像描述

您只需以這種方式完成您的第一個 XMLTABLE 查詢 (h):

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

您正在嘗試包含兩個屬性,它們使用@前綴訪問; 因此您的查詢可以修改為:

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<>小提琴

在這種情況下,您也可以在單個 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<>小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM