2

给定的是“XYZ”表中“my_xml”列的值

<?xml version="1.0" encoding="UTF-8"?>
<India>
  <city>
    <string>ADI</string>
    <string>Ahmedabad</string>
  </city>
  <city>
    <string>GNR</string>
    <string>Gandhinagar</string>
  </city>
  <city>
    <string>PUN</string>
    <string>Pune</string>
  </city>
  <city>
    <string>RJT</string>
    <string>Rajkot</string>
  </city>
</India>

我正在尝试提取第一个节点值string所在的第二个string节点的值ADI

输出应仅为“艾哈迈达巴德”

失败的尝试:

select t.my_xml.extract('/India/city/string[2]/text()').getStringVal() from XYZ t where t.my_xml.existsNode('/India/city[string[1] = "ADI"]') = 1;

上述查询的输出是AhmedabadGandhinagarPuneRajkot

预期输出:Ahmedabad

如何在string此处提取节点的特定节点值?

4

3 回答 3

2

您要选择将 ADI 文本作为第一个字符串的节点

试试这个:

select 
    t.my_xml.extract('//city[./string[1]/text() = "ADI"]/string[2]/text()').getStringVal()
from XYZ t
    where t.my_xml.existsNode('/India/city[string[1] = "ADI"]') = 1;
于 2017-01-10T13:10:07.020 回答
2

用于XMLTable提取值:

SELECT t.*
FROM   XYZ x,
       XMLTable(
         '/India/city'
         PASSING x.my_xml
         COLUMNS string1 CHAR(3)      PATH './string[1]',
                 string2 VARCHAR2(20) PATH './string[2]'
       ) t
WHERE  t.string1 = 'ADI';
于 2017-01-10T13:28:44.070 回答
0

1) 使用 xmlquery 和 flwor xquery

select xmlcast( xmlquery('for $i in ./India/city where $i//string[1]/text() = $cond return $i/string[2]/text()' passing xmltype(q'~<India>
  <city>
    <string>ADI</string>
    <string>Ahmedabad</string>
  </city>
  <city>
    <string>GNR</string>
    <string>Gandhinagar</string>
  </city>
  <city>
    <string>PUN</string>
    <string>Pune</string>
  </city>
  <city>
    <string>RJT</string>
    <string>Rajkot</string>
  </city>
</India>~'), 'ADI' as "cond"    returning content) as varchar2(20)) result  from dual; 

2) 如果您期望不止一行,请尝试使用 xmltable。

select *  from xmltable('$doc/India/city' passing xmltype(q'~<India>
  <city>
    <string>ADI</string>
    <string>Ahmedabad</string>
  </city>
  <city>
    <string>GNR</string>
    <string>Gandhinagar</string>
  </city>
  <city>
    <string>PUN</string>
    <string>Pune</string>
  </city>
   <city>
    <string>ADI</string>
    <string>Ahmedabad</string>
  </city>
  <city>
    <string>RJT</string>
    <string>Rajkot</string>
  </city>
</India>~') as "doc" 
columns 
   string1 varchar2(20) path'./string[1]/text()'
   , string2 varchar2(20) path'./string[2]/text()'

)
where string1='ADI'

和带有 flwor xquery 的 xmltable

select column_value  from xmltable('for $el in $doc/India/city where $el//string[1]/text() = "ADI" return $el/string[2]/text()' passing xmltype(q'~<India>
  <city>
    <string>ADI</string>
    <string>Ahmedabad</string>
  </city>
  <city>
    <string>GNR</string>
    <string>Gandhinagar</string>
  </city>
  <city>
    <string>PUN</string>
    <string>Pune</string>
  </city>
   <city>
    <string>ADI</string>
    <string>Ahmedabad</string>
  </city>
  <city>
    <string>RJT</string>
    <string>Rajkot</string>
  </city>
</India>~') as "doc" )
;
于 2017-01-10T13:37:40.450 回答