如何提取这些子串?

时间:2011-06-16 06:57:08

标签: ruby-on-rails ruby-on-rails-3 string xpath nokogiri

在Ruby 1.9.2和Rails 3.0.1中,我有这个字符串:

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
  <en-note><div>this is new note content</div>
  <div></div>
  <div></div>
  <div><br clear="none"/><en-media width="640" height="480" style="cursor: url('/images/magnify.cur'),crosshair;" hash="6d7b2488610acd5d1269cb466567dcc6" type="image/jpeg"></en-media></div>
  <div></div>
  <div>some more text</div>
  <div></div>
  <div></div><br/><en-media hash="da3ad4553701959b5c5620fb609af9c4" type="image/jpeg"/></en-note>

在字符串中有两个en-media标记,每个标记都有hash属性。

我还有一个哈希值数组。我需要使用这些哈希值来替换相关的en-media标记。

例如,数组中的第一个元素是"6d7b2488610acd5d1269cb466567dcc6"。我需要使用此值从字符串中替换此子字符串:

<en-media width="640" height="480" style="cursor: url('/images/magnify.cur'),crosshair;" hash="6d7b2488610acd5d1269cb466567dcc6" type="image/jpeg"></en-media>

我该怎么做?

我尝试使用Nokogiri如下:

string.xpath("//*[@*[hash]]")

但我得到了这个结果:

--- !ruby/object:Nokogiri::XML::NodeSet 
document: !ruby/object:Nokogiri::HTML::Document 
  decorators: 
  errors: 
  - !ruby/exception:Nokogiri::XML::SyntaxError 
    message: Tag en-note invalid
    code: 801
    column: 9
    domain: 5
    file: 
    int1: 0
    level: 2
    line: 3
    str1: en-note
    str2: 
    str3: 
  - !ruby/exception:Nokogiri::XML::SyntaxError 
    message: Tag en-media invalid
    code: 801
    column: 170
    domain: 5
    file: 
    int1: 0
    level: 2
    line: 6
    str1: en-media
    str2: 
    str3: 
  - !ruby/exception:Nokogiri::XML::SyntaxError 
    message: Tag en-media invalid
    code: 801
    column: 84
    domain: 5
    file: 
    int1: 0
    level: 2
    line: 10
    str1: en-media
    str2: 
    str3: 
  node_cache: 
  - !ruby/object:Nokogiri::XML::Element {}

  - !ruby/object:Nokogiri::XML::Element {}

1 个答案:

答案 0 :(得分:0)

要获取哈希属性,您是否尝试过string.xpath("en-note//en-media//@hash")? 也许您想要像这样修改XML:

string.xpath("en-note//en-media//@hash").each_with_index do |attribute, index|
  attribute.value = values[index]
end