【问题标题】:Oracle XMLTYPE extract node depth level numberOracle XMLTYPE 提取节点深度级别数
【发布时间】:2021-05-18 07:51:04
【问题描述】:
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*

这是一个扩展请求另一个question.

我有如下 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>
        <S_DAYS FARE="20" 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
,node_level
 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',
                    node_level      for ordinality
            ) b;

我正在尝试获取节点深度/级别数。 在这里,我有 2 个 S_DAYS 节点。我想按顺序打印关卡编号。

实际结果:

预期结果:

任何帮助将不胜感激。

【问题讨论】:

    标签: sql xml oracle plsql xmltype


    【解决方案1】:

    FOR ORDINALITY 仅返回生成的行号。所以它必须在你想要计算的级别上生成。

    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>
            <S_DAYS FARE="20" 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
    ,x.node_level
     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'
                 passing h.attributes
                 columns 
                        node_level      for ordinality,
                        attributes xmltype path '/S_DAYS'
                ) x
        cross join xmltable(xmlnamespaces(default 'http://www.w3.org/2001/XMLSchema'),
                 'S_DAYS/STUDENT/DIVISION'
                 passing x.attributes
                 columns 
                        ORIGINAL number path  '@ORIGINAL',
                        Code            varchar2(100) path '@Code',
                        S_CODE  number path '@S_CODE'
                ) b;
    

    【讨论】:

    • 太好了.. 感谢您的解释。
    猜你喜欢
    • 1970-01-01
    • 2010-10-11
    • 2021-05-22
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 2013-07-06
    相关资源
    最近更新 更多