[英]Oracle XMLTYPE extract node depth level number
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>
<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 節點。 我想按順序打印級別編號。
實際結果:
預期結果:
任何幫助都感激不盡。
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.