2013-01-15 237 views
0

我需要循環並獲取屬於每個批次的更新,但是當我使用xmldoc.xpath("//update")時,即使沒有嵌套到批次中,它也會給我所有更新。我怎樣才能獲得嵌套到選定批次的更新?XPath嵌套節點

lots = xmldoc.xpath("//lot") 

lots.each do |lot| 
    @lot = Lot.new 
    @lot.legacy_id = lot.at("legacy_id").text 
    @lot.event = @event 
    @lot.group = lot.at("group").text 
    @lot.number = lot.at("number").text 
    @lot.title = lot.at("title").text 
    @lot.description = lot.at("description").text 
    @lot.price = lot.at("price").text 
    @lot.start_at = lot.at("start_at").text 
    @lot.end_at = lot.at("end_at").text 
    @lot.position = lot.at("position").text 
    @lot.notes = lot.at("notes").text 
    @lot.display_type = lot.at("display_type").text 
    @lot.extension_time_in_minutes = lot.at("extension_time_in_minutes").text 
    @lot.status = lot.at("status").text 
    @lot.units = lot.at("units").text 
    @lot.quantity = lot.at("quantity").text 
    @lot.featured = lot.at("featured").text 
    @lot.approved = lot.at("approved").text 
    @lot.base_price = lot.at("base_price").text 
    @lot.buy_now_price = lot.at("buy_now_price").text 
    @lot.reserve_price = lot.at("reserve_price").text 
    @lot.minimum_bid_increment = lot.at("minimum_bid_increment").text 
    @lot.comments = lot.at("comments").text 
    @lot.bid_history = lot.at("bid_history").text 
    @lot.notify_when_outbid = lot.at("notify_when_outbid").text 
    @lot.show_current_bid = lot.at("show_current_bid").text 
    @lot.show_price_post_sale = lot.at("show_price_post_sale").text 
    @lot.save 

    updates = xmldoc.xpath("//update") 
    updates.each do |update| 
    @lot_update = LotUpdate.new 
    @lot_update.lot_id = @lot.id 
    @lot_update.details = update.at("details").text 
    @lot_update.save 
    end 
end 

XML:

<?xml version="1.0" encoding="UTF-8" ?> 
<event> 
    <legacy_id>54321</legacy_id> 
    <lots> 
     <lot> 
      <legacy_id>12345</legacy_id> 
      <number>1</number> 
      <title>Big Cow</title> 
      <description>A big cow</description> 
      <position>1</position> 
      <price>500</price> 
      <start_at>2013-02-15 10:00:00</start_at> 
      <end_at>2013-02-15 12:00:00</end_at> 
      <group>1</group> 
      <notes>More Notes</notes> 
      <display_type>Image</display_type> 
      <extension_time_in_minutes>5</extension_time_in_minutes> 
      <status>open</status> 
      <units>1</units> 
      <quantity>1</quantity> 
      <featured>1</featured> 
      <approved>1</approved> 
      <base_price>1.00</base_price> 
      <buy_now_price>100.00</buy_now_price> 
      <reserve_price>50</reserve_price> 
      <minimum_bid_increment>20</minimum_bid_increment> 
      <comments>Good Cow</comments> 
      <bid_history>Do not show</bid_history> 
      <notify_when_outbid>1</notify_when_outbid> 
      <show_current_bid>1</show_current_bid> 
      <show_price_post_sale>0</show_price_post_sale> 
      <updates> 
       <update> 
        <lot_id>12345</lot_id> 
        <details>Color is blue</details> 
       </update> 
      </updates> 
      <images> 
       <image>http://the.image.is/here.jpg</image> 
      </images> 
      <catalog_images> 
       <catalog_image>http://the.image.is/here.jpg</catalog_image> 
      </catalog_images> 
      <videos> 
       <video>http://the.video.is/here.flv</video> 
      </videos> 
     </lot> 
    </lots> 
    <lots> 
     <lot> 
      <legacy_id>12346</legacy_id> 
      <number>1</number> 
      <title>Small Cow</title> 
      <description>A small cow</description> 
      <position>1</position> 
      <price>500</price> 
      <start_at>2013-02-15 10:00:00</start_at> 
      <end_at>2013-02-15 12:00:00</end_at> 
      <group>1</group> 
      <notes>More Notes</notes> 
      <display_type>Image</display_type> 
      <extension_time_in_minutes>5</extension_time_in_minutes> 
      <status>open</status> 
      <units>1</units> 
      <quantity>1</quantity> 
      <featured>1</featured> 
      <approved>1</approved> 
      <base_price>1.00</base_price> 
      <buy_now_price>100.00</buy_now_price> 
      <reserve_price>50</reserve_price> 
      <minimum_bid_increment>20</minimum_bid_increment> 
      <comments>Really good Cow</comments> 
      <bid_history>Do not show</bid_history> 
      <notify_when_outbid>1</notify_when_outbid> 
      <show_current_bid>1</show_current_bid> 
      <show_price_post_sale>0</show_price_post_sale> 
      <updates> 
       <update> 
        <lot_id>12345</lot_id> 
        <details>Color is purple</details> 
       </update> 
      </updates> 
      <images> 
       <image>http://the.image.is/here.jpg</image> 
      </images> 
      <catalog_images> 
       <catalog_image>http://the.image.is/here.jpg</catalog_image> 
      </catalog_images> 
      <videos> 
       <video>http://the.video.is/here.flv</video> 
      </videos> 
     </lot> 
    </lots> 
</event> 
+0

請添加一個示例xml和您的預期輸入/輸出 – JWiley

+1

這是在無數StackOverflow問題中所犯的同樣的錯誤。我確定有很多重複的東西。在XPath中,''//並不意味着「從這裏開始」。它意味着「從文檔根開始」。你只需要'update'或'.// update',這取決於你是在尋找一個直接的孩子還是任何深度。 –

+0

我想每次都有1個批次和幾個更新。我嘗試過。更新,它給了我相同的輸出。它將所有更新放入所有批次中,只是更新不會給我提供任何幫助。 – user1977840

回答

1

你想lot.xpath('.//update')

+1

或者'lot.css('update')'。雖然這個CSS表達式並不固有地說「僅當前節點的後代」(因爲CSS不能這麼說),但Nokogiri中的實現是'someElement.css(...)'總是隻看到someElement的後代'。 – Phrogz