How can I extract these substrings?
In Ruby 1.9.2 and Rails 3.0.1, I have this string:
<?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>
<开发者_Python百科div></div>
<div>some more text</div>
<div></div>
<div></div><br/><en-media hash="da3ad4553701959b5c5620fb609af9c4" type="image/jpeg"/></en-note>
In the string there are two en-media
tags, each of which has a hash
attribute.
I also have an array of hash values. I need to use these hash values to replace the relevant en-media
tags.
For example, the first element in the array is "6d7b2488610acd5d1269cb466567dcc6"
. I need to use this value to replace this substring from the the string:
<en-media width="640" height="480" style="cursor: url('/images/magnify.cur'),crosshair;" hash="6d7b2488610acd5d1269cb466567dcc6" type="image/jpeg"></en-media>
How can I do this?
I tried using Nokogiri as follows:
string.xpath("//*[@*[hash]]")
But I get this result:
--- !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 {}
To get the hash attributes have you tried string.xpath("en-note//en-media//@hash")
?
Perhaps you want to modify the XML like this:
string.xpath("en-note//en-media//@hash").each_with_index do |attribute, index|
attribute.value = values[index]
end
精彩评论