'Select value from xml in hive/spark with xpath

I have xml:

<info>
<infofield name "date">2022-01-01</infofield>
<infofield name "country">USA</infofield>
<infofield name "city">New York</infofield>
</info>

I want select "New York" with xpath, but when i am write this query: Select xpath_string(body,'info/infofield[3]/@name') my result is - "city", how i can select "New York"?



Solution 1:[1]

The easy way:

info/infofield[3]/text()

If you want to be shore that if the sequence of infofields changes, you still get te city this is the better way:

info/infofield[@name='city']/text()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Siebe Jongebloed