0

我需要循环并获取属于每个批次的更新,但是当我使用xmldoc.xpath("//update")它时,即使没有嵌套到批次中,它也只会给我所有的更新。如何获取仅嵌套在选定批次中的更新?

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.save

  updates = xmldoc.xpath("//update")
  updates.each do |update|
    @lot_update = LotUpdate.new
    @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>
        </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>
        </lot>
    </lots>
</event>
4

1 回答 1

1

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

于 2013-01-15T16:29:17.497 回答