Nokogiri는 색소폰 파서를 사용하여 HTML 조각을 구문 분석 할 수 있습니까?
문제
이 코드가 있습니다.
class MyParser < Nokogiri::XML::SAX::Document
def characters(string)
LOG.debug("characters #{string}")
end
def start_element(name, attrs = [])
LOG.debug("start_element #{name}")
end
def end_element(name)
LOG.debug("end_element #{name}")
end
end
parser = Nokogiri::HTML::SAX::Parser.new(MyParser.new)
parser.parse(File.new($*[0], 'rb'))
이와 같은 HTML 조각에서 실행하고
<h1>Hello</h1>
<p>Hi.</p>
출력은 첫 번째 요소 만 처리된다는 것을 보여줍니다.
start_element h1
characters Hello
end_element h1
조각을 감싸면 html
그리고 body
태그, 전체 입력이 구문 분석됩니다.
HTML 조각에서 색소폰 스타일 파서를 사용하는 방법이 있습니까?
해결책
루트 요소로 조각을 감싸 야합니다.
<div>
<h1>Hello</h1>
<p>Hi.</p>
</div>
문제를 해결해야합니다.
제휴하지 않습니다 StackOverflow