如何检索nokogiri处理指令属性?
我正在使用Nokogiri解析XML。
我能够检索样式表。 但不是每个样式表的属性。
1.9.2p320 :112 >style = xml.xpath('//processing-instruction("xml-stylesheet")').first => # style.name => "xml-stylesheet" style.content => "type=\"text/xsl\" href=\"CDA.xsl\""
有没有简单的方法来获取类型,href属性值?
要么
唯一的方法是解析处理指令的内容(style.content)?
我按照下面的答案解释了这个问题。
Nokogiri可以搜索“?xml-stylesheet”标签吗?
为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
你不能那样做吗?
style.content.attribute['type'] # or attr['type'] I am not sure style.content.attribute['href'] # or attr['href'] I am not sure
检查此问题如何使用Nokogiri访问属性 。