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ノードの値がある2番目のノードの値を抽出しようとしていますADI

出力は「Ahmedab​​ad」のみにする必要があります

失敗した試行:

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 に答える