Given is the value of "my_xml" column in "XYZ" table
<?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>
I am trying to extract value of second string
node where first string
node value is ADI
Output should be "Ahmedabad" only
Failed attempts:
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;
Output for above query is AhmedabadGandhinagarPuneRajkot
Expected output: Ahmedabad
How to extract specific node value for string
node here?
You want to select the node that has the ADI text as first string.
Try this:
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;