1

I'm trying to use XPath to extract a node from a XML file, by looking for a specific value in one of its subnodes.

I have a XML file from this location: Hourly forecasts for selected locations

Here is a part from it:

<HourlyLocationsForecast> <Identification> <Organization>Israel Meteorological Service</Organization> <Title>Hourly forecasts for selected locations</Title> <IssueDateTime>Tue Dec 10 06:30:40 IST 2019</IssueDateTime> </Identification> <Location> <LocationMetaData> <LocationName>AFULA NIR HAEMEQ</LocationName> <LocationLatitude>32.596</LocationLatitude> <LocationLongitude>35.277</LocationLongitude> <LocationHeight>60</LocationHeight> </LocationMetaData> <LocationData> <Forecast> <ForecastTime>10/12/2019 04:00 UTC</ForecastTime> <Temperature>11.3</Temperature> <RelativeHumidity>100</RelativeHumidity> <WindSpeed>1.4</WindSpeed> <WindDirection>34</WindDirection> </Forecast> <Forecast> <ForecastTime>10/12/2019 05:00 UTC</ForecastTime> <Temperature>11.7</Temperature> <RelativeHumidity>100</RelativeHumidity> <WindSpeed>1.5</WindSpeed> <WindDirection>31</WindDirection> </Forecast> <Forecast> <ForecastTime>10/12/2019 06:00 UTC</ForecastTime> <Temperature>13.8</Temperature> <RelativeHumidity>100</RelativeHumidity> <WindSpeed>1.3</WindSpeed> <WindDirection>27</WindDirection> </Forecast> </LocationData> </Location> <Location> <LocationMetaData> <LocationName>AVDAT</LocationName> <LocationLatitude>30.788</LocationLatitude> <LocationLongitude>34.771</LocationLongitude> <LocationHeight>555</LocationHeight> </LocationMetaData> <LocationData> <Forecast> <ForecastTime>10/12/2019 04:00 UTC</ForecastTime> <Temperature>10.5</Temperature> <RelativeHumidity>95</RelativeHumidity> <WindSpeed>1.2</WindSpeed> <WindDirection>238</WindDirection> </Forecast> <Forecast> <ForecastTime>10/12/2019 05:00 UTC</ForecastTime> <Temperature>11.6</Temperature> <RelativeHumidity>91</RelativeHumidity> <WindSpeed>1.4</WindSpeed> <WindDirection>251</WindDirection> </Forecast> <Forecast> <ForecastTime>10/12/2019 06:00 UTC</ForecastTime> <Temperature>13.9</Temperature> <RelativeHumidity>83</RelativeHumidity> <WindSpeed>1.5</WindSpeed> <WindDirection>258</WindDirection> </Forecast> </LocationData> </Location> <Location> <LocationMetaData> <LocationName>BEER SHEVA</LocationName> <LocationLatitude>31.251</LocationLatitude> <LocationLongitude>34.799</LocationLongitude> <LocationHeight>279</LocationHeight> </LocationMetaData> <LocationData> <Forecast> <ForecastTime>10/12/2019 04:00 UTC</ForecastTime> <Temperature>12.2</Temperature> <RelativeHumidity>98</RelativeHumidity> <WindSpeed>0.7</WindSpeed> <WindDirection>136</WindDirection> </Forecast> <Forecast> <ForecastTime>10/12/2019 05:00 UTC</ForecastTime> <Temperature>12.6</Temperature> <RelativeHumidity>95</RelativeHumidity> <WindSpeed>0.7</WindSpeed> <WindDirection>145</WindDirection> </Forecast> </LocationData> </Location> </HourlyLocationsForecast> 

I want to extract full weather forecast just for a specific city. The city name is stored under /Location/LocationMetaData/LocationName, while the actuael data is under /Location/LocationData.

After looking at this question: XPath: Select first element with a specific attribute, I tried to use this XPath string:

//HourlyLocationsForecast/Location[@LocationMetaData/LocationName='JERUSALEM CENTRE'] 

I assumed I can relate to a value in a subnode as well as a value of an attribute in the node itself.

I get 'No Match' as an answer.

What am I doing wrong?

1 Answer 1

2

Two problems:

  1. LocationMetaData is an element, not an attribute, so remove the preceding @.
  2. JERUSALEM CENTRE appears nowhere in your XML at all, so filter instead on a LocationName that actually exists (e.g. AFULA NIR HAEMEQ).

This corrected XPath,

//HourlyLocationsForecast/Location[LocationMetaData/LocationName='AFULA NIR HAEMEQ'] 

would actually select something given your XML.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that! I removed the @ and got exactly what I wanted. Now it's //HourlyLocationsForecast/Location[LocationMetaData/LocationName='JERUSALEM CENTRE']. And about the 'JERUSALEM CENTRE' - you're right. it's in the full file (which I linked into) and not in the snippest I've pasted here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.