Created
April 6, 2012 04:34
-
-
Save kimoto/2316890 to your computer and use it in GitHub Desktop.
RubyのHTML/XMLパーサー、nokogiriの間違えやすい問題について
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# RubyのHTML/XMLパーサー、nokogiriの間違えやすい問題について | |
# Nokogiri関数に渡すXMLデータのどこかのタグの属性値に"HTML"という文字が含まれていると、 | |
# HTML文書として解釈され、<html><body>というのが勝手に挿入される。そのため本来マッチするはずのXPathがマッチしなくなったりする。 | |
# | |
# Nokogiri::XMLかNokogiri::HTMLを使いましょう! (当たり前) | |
# でも属性値にHTMLという文字が含まれてるだけでHTML文書として解釈するのはまずい気がする。。 | |
# | |
# ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin10.8.0] | |
# nokogiri (1.5.0) | |
require 'nokogiri' | |
xml = <<EOT | |
<Items> | |
<Item key="HTML"></Item> | |
</Items> | |
EOT | |
puts Nokogiri(xml).to_s | |
# => <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> | |
# <html><body><items><item key="HTML"></item></items></body></html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment