2012-10-25 2 views
2

Nokogiri를 사용하여 XML을 구문 분석하고 있습니다.nokogiri 처리 명령어 속성을 검색하는 방법은 무엇입니까?

스타일 시트를 가져올 수 있습니다. 그러나 각 스타일 시트의 속성은 아닙니다.

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 속성 값?

OR

유일한 방법은 처리 명령의 내용 (style.content)를 분석하는 것이다?

+0

파싱하려는 XML의 짧은 샘플. –

답변

3

아래의 지침에 따라이 문제를 해결했습니다. 노코 기리 :: XML :: ProcessingInstruction 클래스에

Can Nokogiri search for "?xml-stylesheet" tags?

추가 된 새로운 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 
당신이 포함 원하는 경우 그것은 도움이 될
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.

+0

이것은 String error에 대해 정의되지 않은 메소드 속성 또는 attr을 제공합니다. –

+0

답장을 보내 주셔서 감사합니다. –

관련 문제