0

フォーマットに一致する選択したノードの子を取得するのに問題があります。どういうわけか、選択した形式からではなく、形式のグローバルな選択を返します。

  doc = Nokogiri::XML(f)
  group_with_obj_and_unit =  doc.xpath("/x:svg/x:g[x:text[matches(text(),'^#[0123456789]+\-[0123456789][\/[0123456789]+]*$')]][x:text[not(matches(text(),'^#[0123456789]+\-[0123456789][\/[0123456789]+]*$'))]][x:path or x:rect or x:circle or x:ellipse or x:polyline or x:polygon]", FindWithRegex, "x" => "http://www.w3.org/2000/svg")
  group_with_obj_and_unit.each do |matched_group|
    text_object = matched_group.xpath("//x:g/x:text[matches(text(),'^#[0123456789]+\-[0123456789][\/[0123456789]+]*$')]", FindWithRegex, "x" => "http://www.w3.org/2000/svg")
    object = matched_group.xpath("//x:g/x:path | /x:svg/x:g/x:rect | /x:svg/x:g/x:circle | /x:svg/x:g/x:ellipse | /x:svg/x:g/x:polyline | /x:svg/x:g/x:polygon", "x" => "http://www.w3.org/2000/svg")

    #object['id'] = text_object.text
  end

編集済み

@pguardiario の提案後

  def run!
    fileContent = File.new @file_path, "r"
    file = File.open @file_path, "r+"
    doc = Nokogiri::XML(fileContent)

    group_with_obj_and_unit =  doc.xpath("/x:svg/x:g[x:text[matches(text(),'^#[0123456789]+\-[0123456789][\/[0123456789]+]*$')]][x:text[not(matches(text(),'^#[0123456789]+\-[0123456789][\/[0123456789]+]*$'))]][x:path or x:rect or x:circle or x:ellipse or x:polyline or x:polygon]", FindWithRegex, "x" => "http://www.w3.org/2000/svg")
    group_with_obj_and_unit.each do |matched_group|
      #find the text object and the object of the selected node
      text_object = matched_group.at_xpath("./x:text[matches(text(),'^#[0123456789]+\-[0123456789][\/[0123456789]+]*$')]", FindWithRegex, "x" => "http://www.w3.org/2000/svg")
      object = matched_group.at_xpath("./x:path | ./x:rect | ./x:circle | ./x:ellipse | ./x:polyline | ./x:polygon", "x" => "http://www.w3.org/2000/svg")

      object['id'] = text_object.text
    end
    file.write doc.to_xml

    file.close
  end
4

1 に答える 1

2
  • //ルートに続く任意の場所に一致することを意味します
  • .//は、現在のノードに続く任意の場所に一致することを意味します。
于 2012-07-02T09:04:37.260 に答える