0

Let's try to parse and extract node from Yahoo Weather RSS.

URL is here http://weather.yahooapis.com/forecastrss?w=12602818&u=c

xmllint has to be used for this purpose.

Here is the node to extract:

<yweather:condition  text="Partly Cloudy"  code="30"  temp="3"  date="Fri, 16 Nov 2014 8:10 am AST" />

The problem

A naive xmllint --xpath "//yweather:condition" the_rss_file_from_yahoo replied with an error:

XPath error : Undefined namespace prefix
xmlXPathEval: evaluation failed
XPath evaluation failure

It has to comes from the fact there's a namespace defined. But we're stuck here.

Is it possible to get this node with xmllint, on the command line? And if yes, how?

Edit/Answer: As the question was marked duplicated, if you're a XPath newbie like me looking for a one-liner solution, you may use xmllint filename --xpath "//*[local-name()='condition']", with two slashes contrarily to the other question, which was using one (because it was accessing the root node). However for the formal way to address this, see answer A) from the original question and use // instead of / in the XPath query (Thanks Tomalek for helping me out).

the_yellow_logo
  • 675
  • 5
  • 13
  • I needed less than 30 seconds to find an duplicate of your question. That's at least 10 times less time than you needed to *write* it. Think about that. – Tomalak Dec 05 '14 at 08:10
  • Hey Tomalak. In fact I read that answer you posted before posting mine. Still it didn't work for me, and it seems I'm not going to get some help from the other question. More importantly, as Yahoo Weather API is very used, I thought asking that precise question (xmllint+Yahoo weather) would help some people out, coming from Google for instance. – the_yellow_logo Dec 05 '14 at 08:21
  • You did not indicade what you have tried w/r/t namespaces, that would have changed matters a bit. However, your question is not at all specific to the Yahoo APIs. It's about dealing with XML namespaces in xmllint, plain and simple. I haven't checked, but I'm sure the documentation tells you all you need once you know the keyword. And that's why this question still is a duplicate. :) – Tomalak Dec 05 '14 at 08:26
  • Besides the question is probably not the same, because xmllint --debug replies with a different structure than his: ELEMENT yweather:condition instead of his ELEMENT chat default namespace href=http://purl.org/net/ulf/ns/0.4-02 . Which is probably why the solution there doesn't work for me (I don't have a namespace indicated as he has). (I understand how they look similar at the surface though. And maybe it's my improper handling of xmllint that make them unsimilar) – the_yellow_logo Dec 05 '14 at 08:27
  • A default namespace is no different than a normal namespace. It just lacks a prefix, that's why the debug output is a little different. The problem remains exactly the same: You must make known that namespace URI before you can issue an xpath query. The commandline options to do that are the same in both cases. – Tomalak Dec 05 '14 at 08:31
  • Well you obviously know better, but his two solutions didn't work for me. For example for his solution B) I tried both `xmllint the_rss_file --xpath "/*[local-name()='condition']"` and `xmllint the_rss_file --xpath "/*[namespace-uri()='http://xml.weather.yahoo.com/ns/rss/1.0' and local-name()='condition']"` and both replied with `XPath set is empty` – the_yellow_logo Dec 05 '14 at 08:35
  • 1
    Now there's what you should have been posting right from the start. Try `xmllint the_rss_file --xpath "//*[local-name()='condition']"` (note the double slash. maybe brush up on some xpath basics, too). But really this solution is sub-optimal. Go for declaring the namespace URI properly. – Tomalak Dec 05 '14 at 08:39
  • As for his solution A) `xmllint --shell the_rss_file`, I typed `setns yweather=http://xml.weather.yahoo.com/ns/rss/1.0` followed by `xpath /yweather:condition` on another line and got `Object is a Node Set : Set contains 0 nodes:`. – the_yellow_logo Dec 05 '14 at 08:39
  • Double slashes, as I said. `/` only matches the root node. – Tomalak Dec 05 '14 at 08:43
  • No, the question is closed, it can't be answered anymore. You got your problem solved, which is good, and future visitors will very likely have the "I don't know how XML namespaces work" problem, so the duplicate is still valid. From my POV there's nothing left to do. – Tomalak Dec 05 '14 at 08:53
  • Okay, I edited my question to help people out but understand your POV, thanks again. – the_yellow_logo Dec 05 '14 at 08:58
  • 1
    Next time: "I have found this other question, and I tried this ... and this ... but it did not work. Where's my error?" That would have sped up things considerably. ;) – Tomalak Dec 05 '14 at 09:13

0 Answers0