我正在从一个XML文件中读取一些内容,其中包含以下链接:
<wcm:root xmlns:wcm="http://www.stellent.com/wcm-data/ns/8.0.0" version="8.0.0.0">
<wcm:element name="NotesToEditors">
<a href="ssNODE/something">Something</a>
<a href="ssNODE/hello">hello</a>
<a href="https//:www.linkkkk.com">linkkkk</a>
</wcm:element> 读取文件:
page_notes_to_editors = doc.xpath("/wcm:root/wcm:element[@name='NotesToEditors']").inner_text进行清理工作:
notes = Nokogiri::XML.fragment(page_notes_to_editors)
notes.css('a[href="ssNODE]')
.each{|a| a.replace("<p>#{a.content}</p>")}我试着像这样逃避双引号:
notes.css(a["href=\"ssNODE]")它还在抱怨。
但是,当字符串中包含奇怪的字符时,这是不起作用的。这是我得到的错误:
`on_error': unexpected '"' after 'equal'我想要的结果是将ssNODE链接转换为保留文本的段落。
有谁对如何达到我想要的结果有什么建议吗?
发布于 2013-11-12 13:14:23
在代码notes.css('a[href="ssNODE]')中,您忽略了"。把它写成notes.css('a[href^="ssNODE"]')
这里有文档,CSS [attribute^=value] Selector
[attribute^=value]选择器匹配属性值以指定值开头的每个元素。
https://stackoverflow.com/questions/19926261
复制相似问题