Nokogiri在xpath查询中返回空

时间:2015-08-24 08:12:20

标签: ruby-on-rails ruby nokogiri

我从外部服务获得以下HTTP响应正文。

<?xml version="1.0" encoding="utf-8"?>
<ParcelShopSearchResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://gls.dk/webservices/">
  <accuracylevel>EXACT</accuracylevel>
  <parcelshops>
    <PakkeshopData>
      <Number>97853</Number>
      <CompanyName>7-Eleven Artillerivej</CompanyName>
      <Streetname>Artillerivej 50</Streetname>
      <Streetname2>Pakkeshop: 97853</Streetname2>
      <ZipCode>2300</ZipCode>
      <CityName>København S</CityName>
      <CountryCode>008</CountryCode>
      <CountryCodeISO3166A2>DK</CountryCodeISO3166A2>
      <Telephone>-</Telephone>
      <Longitude>12.5835</Longitude>
      <Latitude>55.6643</Latitude>
      <OpeningHours>
        <Weekday>
          <day>Monday</day>
          <openAt>
            <From>00:00</From>
            <To>24:00</To>
          </openAt>
          <breaks />
        </Weekday>
        <Weekday>
          <day>Tuesday</day>
          <openAt>
            <From>00:00</From>
            <To>24:00</To>
          </openAt>
          <breaks />
        </Weekday>
        <Weekday>
          <day>Wednesday</day>
          <openAt>
            <From>00:00</From>
            <To>24:00</To>
          </openAt>
          <breaks />
        </Weekday>
        <Weekday>
          <day>Thursday</day>
          <openAt>
            <From>00:00</From>
            <To>24:00</To>
          </openAt>
          <breaks />
        </Weekday>
        <Weekday>
          <day>Friday</day>
          <openAt>
            <From>00:00</From>
            <To>24:00</To>
          </openAt>
          <breaks />
        </Weekday>
        <Weekday>
          <day>Saturday</day>
          <openAt>
            <From>00:00</From>
            <To>24:00</To>
          </openAt>
          <breaks />
        </Weekday>
        <Weekday>
          <day>Sunday</day>
          <openAt>
            <From>00:00</From>
            <To>24:00</To>
          </openAt>
          <breaks />
        </Weekday>
      </OpeningHours>
    </PakkeshopData>
  </parcelshops>
</ParcelShopSearchResult>

然而,如果我执行以下查询,它会返回[],即使它显然应该返回EXACT

doc      = Nokogiri::XML(response_body)
accuracy = doc.xpath('//accuracylevel').try(:text)

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

命名空间问题。

doc.xpath('//xmlns:accuracylevel', doc.collect_namespaces)

或更明确地说,

doc.xpath('//webserv:accuracylevel', { webserv: "http://gls.dk/webservices/" })