簡體   English   中英

Oracle XMLTYPE提取第一個節點

[英]Oracle XMLTYPE extract first node

我在從Oracle中的XML的特定元素提取第一個節點時遇到問題。

這是XML:

<data type="TestData" version="1">
  <MasterTable Name="TestMaster"/>
  <Table Name="TestTable1">Test</Table>
  <Table Name="TestTable2"/>
  <Table Name="TestTable3"/>
  <Table Name="TestTable4"/>
  <Table Name="TestTable5"/>
  <Table Name="TestTable6"/>
  <Table Name="TestTable7"/>
  <Table Name="TestTable8"/>
  <Table Name="TestTable9"/>
  <Table Name="TestTable10"/>
  <Table Name="TestTable11"/>
  <Table Name="TestTable12"/>
  <Table Name="TestTable13"/>
  <Table Name="TestTable14"/>
  <Fact Name="TestFact1"/>
</data>

我試圖提取第一個Name元素“ TestTable1”的值和第一個Name元素“ Test”的文本。

我有以下查詢,它們只是返回null:

select a.xml.extract('//Name[1]') from my_table a; --Attempting to return "TestTable1" from Name attribute 1



select a.xml.extract('//Name[1]/text()') from my_table a; --Attempting to return the text "Test" from Name attribute 1
with test as( select xmltype('<data type="TestData" version="1">
  <MasterTable Name="TestMaster"/>
  <Table OtherName="TestTable1">OtherTable</Table>
  <Table Name="TestTable1">Test</Table>
  <Table Name="TestTable2"/>
  <Table Name="TestTable3"/>
  <Table Name="TestTable4"/>
  <Table Name="TestTable5"/>
  <Table Name="TestTable6"/>
  <Table Name="TestTable7"/>
  <Table Name="TestTable8"/>
  <Table Name="TestTable9"/>
  <Table Name="TestTable10"/>
  <Table Name="TestTable11"/>
  <Table Name="TestTable12"/>
  <Table Name="TestTable13"/>
  <Table Name="TestTable14"/>
  <Fact Name="TestFact1"/>
</data>') x from dual)


select t.x.extract('//Table[@Name][1]'),t.x.extract('//Table[1]'),  t.x.extract('//Table[@Name][1]/text()'),t.x.extract('//Table[1]/text()'),t.x.extract('//Table[./text()][1]') from test t;

txextract('//Table[@Name][1]')返回第一個'Table'; 屬性名稱為txextract('//Table[1]')返回第一個'Table';
txextract('//Table[./text()][1]')返回第一個非空表元素;

您可以使用XMLTABLE

SQL小提琴

Oracle 11g R2架構設置

CREATE TABLE table_name ( xml ) as
select xmltype('<data type="TestData" version="1">
  <MasterTable Name="TestMaster"/>
  <Table OtherName="TestTable1">OtherTable</Table>
  <Table Name="TestTable1">Test</Table>
  <Table Name="TestTable2"/>
  <Table Name="TestTable3"/>
  <Table Name="TestTable4"/>
  <Table Name="TestTable5"/>
  <Table Name="TestTable6"/>
  <Table Name="TestTable7"/>
  <Table Name="TestTable8"/>
  <Table Name="TestTable9"/>
  <Table Name="TestTable10"/>
  <Table Name="TestTable11"/>
  <Table Name="TestTable12"/>
  <Table Name="TestTable13"/>
  <Table Name="TestTable14"/>
  <Fact Name="TestFact1"/>
</data>') from dual;

查詢1

SELECT x.*
FROM   table_name t
       CROSS JOIN
       XMLTABLE(
         '/data/Table[@Name][1]'
         PASSING t.xml
         COLUMNS Name VARCHAR2(20) PATH '@Name'
       ) x

結果

|       NAME |
|------------|
| TestTable1 |

暫無
暫無

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

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