ruby on rails - XPath嵌套节点



我需要循环并获得属于每个批次的更新,但是当我使用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>

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

相关内容

  • 没有找到相关文章

最新更新