2

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

タイプ、href 属性値を取得する簡単な方法はありますか?

また

唯一の方法は、処理命令の内容 (style.content) を解析することですか?

4

2 に答える 2

3

以下の回答の指示に従って、この問題を解決しました。

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
于 2012-10-25T11:45:32.850 に答える
1

あなたはそれをすることができませんか?

style.content.attribute['type'] # or attr['type'] I am not sure
style.content.attribute['href'] # or attr['href'] I am not sure

この質問How to access attributes using Nokogiri を確認してください。

于 2012-10-25T10:47:14.380 に答える