开发者

XPath and Hpricot -- works on some machines, not others?

The following hpricot code successfully extracts the STPeriods in the XML on two of my machines (Vista and an Ubuntu server) but fails on another Ubuntu laptop. All machines have Hpricot v0.82

Any ideas? Totally stumped.

Hpricot code:

  (doc/"WeatherFeed/Location/WxShortTerm/STPeriod").each do |ham_forecast|

XML file

<?xml version="1.0" encoding="ISO-8859-1" ?>
<WeatherFeed>
  <Location ID="EINN" name="Shannon Airport" state="" country="IE" tz="0" tzname="GMT">
    <Astro>
      <AstroPeriod Day="WED" Date="2009-12-16" Sunrise="8:41 AM GMT" Sunset="4:20 PM GMT" Moonrise="9:03 AM GMT" Moonset="4:02 PM GMT" MoonIllum="4%" MoonAge="0" MoonPhase="Waxing Crescent Moon" MoonIcon="1"/>
      <AstroPeriod Day="THU" Date="2009-12-17" Sunrise="8:42 AM GMT" Sunset="4:20 PM GMT" Moonrise="9:46 AM GMT" Moonset="5:05 PM GMT" MoonIllum="10%" MoonAge="1" MoonPhase="Waxing Crescent Moon" MoonIcon="1"/>
    </Astro>
    <WxOb StationID="EINN" TempC="7" TempF="45" ApparentC="5" ApparentF="41" DewPointC="6" DewPointF="43" RelativeHumidity="93" WindSpeedKnots="8" WindSpeedMPH="9" WindDirection="320" WindDirectionEng="NW" WindGustKnots="0" WindGustMPH="0" PressureMB="1013" PressureIN="29.88" Wx="Mostly Cloudy" Icon="mcloudyn.gif" Visibility="&gt;10km" VisibilityKM="16.09" VisibilityMI="10" ReportEpoch="1260999000" ReportDate="2009-12-16 21:30 UTC"/>
    <WxShortTerm ID="EINN">
      <STPeriod interval="1" Epoch="1261018800" Day="Thu" Date="2009-12-17" Time="03:00" Wx="Overcast" Icon="cloudyn.gif" TempC="6" TempF="42" Pop="0" QPFmm="0" QPFin="0" DewPointC="4.4" DewPointF="40" RelativeHumidity="92" WindSpeedKnots="12" WindSpeedMPH="14" WindDirectionEng="NE"开发者_C百科/>
      <STPeriod interval="2" Epoch="1261029600" Day="Thu" Date="2009-12-17" Time="06:00" Wx="Mostly Cloudy" Icon="mcloudyn.gif" TempC="4" TempF="39" Pop="0" QPFmm="0" QPFin="0" DewPointC="3" DewPointF="37" RelativeHumidity="93" WindSpeedKnots="11" WindSpeedMPH="13" WindDirectionEng="NE"/>
      <STPeriod interval="3" Epoch="1261040400" Day="Thu" Date="2009-12-17" Time="09:00" Wx="Partly Cloudy" Icon="pcloudy.gif" TempC="2" TempF="36" Pop="0" QPFmm="0" QPFin="0" DewPointC="1.4" DewPointF="35" RelativeHumidity="95" WindSpeedKnots="10" WindSpeedMPH="12" WindDirectionEng="NE"/>
      <STPeriod interval="4" Epoch="1261051200" Day="Thu" Date="2009-12-17" Time="12:00" Wx="Partly Cloudy" Icon="pcloudy.gif" TempC="4" TempF="38" Pop="0" QPFmm="0" QPFin="0" DewPointC="1.6" DewPointF="35" RelativeHumidity="87" WindSpeedKnots="12" WindSpeedMPH="14" WindDirectionEng="NE"/>
      <STPeriod interval="5" Epoch="1261062000" Day="Thu" Date="2009-12-17" Time="15:00" Wx="Mostly Cloudy" Icon="mcloudy.gif" TempC="3" TempF="37" Pop="0" QPFmm="0" QPFin="0" DewPointC="1" DewPointF="34" RelativeHumidity="87" WindSpeedKnots="12" WindSpeedMPH="14" WindDirectionEng="NE"/>
      <STPeriod interval="6" Epoch="1261072800" Day="Thu" Date="2009-12-17" Time="18:00" Wx="Mostly Cloudy" Icon="mcloudyn.gif" TempC="3" TempF="37" Pop="0" QPFmm="0" QPFin="0" DewPointC="0.9" DewPointF="34" RelativeHumidity="89" WindSpeedKnots="12" WindSpeedMPH="14" WindDirectionEng="NE"/>
      <STPeriod interval="7" Epoch="1261083600" Day="Thu" Date="2009-12-17" Time="21:00" Wx="Mostly Cloudy" Icon="mcloudyn.gif" TempC="2" TempF="36" Pop="0" QPFmm="0" QPFin="0" DewPointC="0.2" DewPointF="32" RelativeHumidity="88" WindSpeedKnots="11" WindSpeedMPH="13" WindDirectionEng="NE"/>
      <STPeriod interval="8" Epoch="1261094400" Day="Fri" Date="2009-12-18" Time="00:00" Wx="Partly Cloudy" Icon="pcloudyn.gif" TempC="-0" TempF="32" Pop="0" QPFmm="0" QPFin="0" DewPointC="-2.1" DewPointF="28" RelativeHumidity="87" WindSpeedKnots="10" WindSpeedMPH="11" WindDirectionEng="NE"/>
    </WxShortTerm>
  </Location>
</WeatherFeed>


Since I couldn't find any enlightenment here or elsewhere, I switched from Hpricot to Nokigiri and it works flawlessly across all machines now. The APIs are almost exactly compatible so it only took <10minutes to switch over. Also, I get the feeling that Nokogiri is being more actively maintained although it doesn't have a dependency on libxml2.


Check the file has the same encoding on each machine. Perhaps something got munged in a transfer?

Try the linux command file -i myfile.txt on each machine and see if you get the same answer.


shouldn't the Hpricot search actually be:

(doc/"/WeatherFeed/Location/WxShortTerm/STPeriod")

Notice additional "/"

Also, when I've had errors with Hpricot failing to find elements, it's been because of a parsing error/malformed document. It's handy to write the Hpricot document out to a text file so you can see if it's doing anything odd, such as closing tags where it shouldn't. (I wonder if that link is the bug you are seeing?).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜