Page 1 of 1

Xpath HELP

Posted: 27 Feb 2016 06:41
by bogdyro
Hello
So I have this XML response from yahoo weather API. I have read some xPath tutorials, tried different ways but I really cannot extract any info from it. Any help please?
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
yahoo:count="1" yahoo:created="2016-02-27T05:47:36Z" yahoo:lang="ro-RO">
<results>
<channel>
<item>
<yweather:condition
xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0"
code="27" date="Fri, 26 Feb 2016 8:50 pm PST"
temp="63" text="Mostly Cloudy"/>
</item>
</channel>
</results>
</query>

How to get 'date', 'temp', 'text' ?
Thanks.

Re: Xpath HELP

Posted: 27 Feb 2016 07:01
by Martin
Hi,

Following script should work to extract the temperature:
temp = evaluateXPathAsString(xml, "/query/results/channel/item/yweather:condition/@temp");

or alternatively:
temp = evaluateXPathAsString(xml, "//yweather:condition/@temp");

There are also some examples available here: Script examples (scroll down to title XML)

Regards,
Martin

Re: Xpath HELP

Posted: 27 Feb 2016 07:45
by bogdyro
Hi Martin. Thanks for the quick reply. Actually none of them work. Tried 2 online evaluators
http://www.utilities-online.info/xpath/#.VtFT-dBaz5c
http://www.freeformatter.com/xpath-tester.html
No text returned. Weird.

Re: Xpath HELP

Posted: 27 Feb 2016 08:30
by bogdyro
Works here
http://codebeautify.org/Xpath-Tester
Also WORKS in automagic.
Thanks Martin !

Re: Xpath HELP

Posted: 27 Feb 2016 09:50
by Martin
Glad it's working. Many online XPath testers don't support namespaces. I'll definitely bookmark the one from codebeautify. Thanks for the link!

Re: Xpath HELP

Posted: 16 Feb 2019 23:36
by jmckeejr
This one a bit harder. I can get the icon for period 1 but can't get the high in fahrenheit. Can anyone help please

Re: Xpath HELP

Posted: 17 Feb 2019 10:02
by Desmanto
Do you mean the high in this?

Code: Select all

</date>
					<period>1</period>
					<high>
						<fahrenheit>36</fahrenheit>
						<celsius>2</celsius>
					</high>
					<low>
						<fahrenheit>18</fahrenheit>
						<celsius>-8</celsius>
					</low>
					<conditions>Clear</conditions>
					
					<icon>clear</icon>
					<icon_url>http://icons.wxug.com/i/c/k/clear.gif</icon_url>
					<skyicon></skyicon>
I just open the xml in notepad++ and use the xml plugin. Click at <fahrenheit>36</fahrenheit> and use the XML Tools > current XML Path. It shows
/response/forecast/simpleforecast/forecastdays/forecastday/high/fahrenheit

You should can get it by using.

Code: Select all

tempf = evaluateXPathAsString(xml, "/response/forecast/simpleforecast/forecastdays/forecastday/high/fahrenheit");
BTW, if the server support json version, it is better to use JSON. As currently, Automagic can handle json much better than xml.

Re: Xpath HELP

Posted: 17 Feb 2019 22:19
by jmckeejr
Thanks I don't get on my laptop often, so I found this online tool that helped a lot and now I have a pretty sweet set of weather widgets.

I used https://xmltoolbox.appspot.com/xpath_generator.html