繁体   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