2009-12-31 1 views
5

나는 &이라는 제목을 가지고 있으며 &이어야합니다. 제목은 doc.at('head/title').inner_html입니다.Nokogiri Ruby에서 HTML을 이스케이프 처리하는 방법 & &

내 원본 문서는 다음과 같습니다

<head><title>Foo & Bar</title></head> 

만에 같이 나오는 다음

>> doc = Nokogiri::HTML.parse(file, nil, "UTF-8") 
>> doc.at('head/title') 
=> #<Nokogiri::XML::Element:0x..fdb851bea name="title" children=#<Nokogiri::XML::Text:0x..fdb850808 "Foo & Bar">> 
>> doc.at('head/title').inner_html 
=> "Foo &amp; Bar" 

내가 좋아하는의 iconv 또는 CGI를 사용하지 않으 :

>> require 'cgi' 
>> CGI.unescapeHTML(doc.at('head/title').inner_html) 
=> "Foo & Bar" 

그것은 추악하고 불편합니다.

답변

7

inner_html 대신 content을 사용하여 콘텐츠를 (X) HTML 대신 일반 텍스트로 가져옵니다.

irb(main):011:0> doc.at('head/title').content 
=> "Foo & Bar"