在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 {}
要获取哈希属性,您尝试过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