我正在使用 Nokogiri 解析 XML。
我能够检索样式表。但不是每个样式表的属性。
1.9.2p320 :112 >style = xml.xpath('//processing-instruction("xml-stylesheet")').first
=> #<Nokogiri::XML::ProcessingInstruction:0x5459b2e name="xml-stylesheet">
style.name
=> "xml-stylesheet"
style.content
=> "type=\"text/xsl\" href=\"CDA.xsl\""
有没有简单的方法来获取 type、href 属性值?
OR
唯一的方法是解析处理指令的内容(style.content)?
我按照下面答案中的说明解决了这个问题。
Nokogiri 可以搜索“?xml-stylesheet”标签吗? https://stackoverflow.com/questions/3542264/can-nokogiri-search-for-xml-stylesheet-tags#answer-12223360
向 Nokogiri::XML::ProcessingInstruction 类添加了新的 to_element 方法
class Nokogiri::XML::ProcessingInstruction
def to_element
document.parse("<#{name} #{content}/>")
end
end
style = xml.xpath('//processing-instruction("xml-stylesheet")').first
element = style.to_element
检索 href 属性值
element.attribute('href').value
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)