如何检索 nokogiri 处理指令属性? [英] How to retrieve the nokogiri processing instruction attributes?

查看:38
本文介绍了如何检索 nokogiri 处理指令属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Nokogiri 解析 XML.

I am parsing the XML using Nokogiri.

我能够检索样式表.但不是每个样式表的属性.

I am able to retrieve the stylesheets. But not the attributes of each stylesheet.

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\""

有没有什么简单的方法可以获取类型、href 属性值?

Is there any easy way to get the type, href attributes values?

唯一的办法就是解析处理指令的内容(style.content)?

Only way is to parse the content(style.content) of the processing instruction ?

推荐答案

我按照以下答案中的说明解决了这个问题.

I solved this problem by following instruction in below answer.

Nokogiri 可以搜索?xml-stylesheet"吗?标签?

向 Nokogiri::XML::ProcessingInstruction 类添加了新的 to_element 方法

Added new to_element method to Nokogiri::XML::ProcessingInstruction class

 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 属性值

To retrieve the href attribute value

 element.attribute('href').value

这篇关于如何检索 nokogiri 处理指令属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆