如何使用 nokogiri 验证 XHTML? [英] How do I validate XHTML with nokogiri?

查看:69
本文介绍了如何使用 nokogiri 验证 XHTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一些帖子暗指您可以使用 nokogiri gem 根据其 DTD 验证 XHTML.虽然我已经成功地使用它来解析 XHTML(寻找a"标签等),但我正在努力验证文档.

对我来说,这个:

doc = Nokogiri::XML(Net::HTTP.get(URI.parse("http://www.w3.org")))放置 doc.validate

导致一整堆:

<预><代码>[#<Nokogiri::XML::SyntaxError: 元素 html 没有声明>,#<Nokogiri::XML::SyntaxError: 元素 html 的属性 xmlns 没有声明>,#<Nokogiri::XML::SyntaxError: 元素 html 的属性 lang 没有声明>,#<Nokogiri::XML::SyntaxError: 元素 html 的属性 lang 没有声明>,#<Nokogiri::XML::SyntaxError: 元素头没有声明>,#<Nokogiri::XML::SyntaxError: 元素头的属性配置文件没有声明[对文档中的每个标签重复.]]

所以我认为这不是正确的方法.我似乎找不到任何好的例子——谁能指出我做错了什么?

我在 Mac OSX 10.5.8 上运行 ruby​​ 1.8.6.Nokogiri 告诉我:

nokogiri: 1.3.3警告:[]库文件:编译:2.6.23加载:2.6.23绑定:扩展

解决方案

不仅仅是你.你正在做的事情应该是正确的做法,但我从来没有运气过.据我所知,Nokogiri 和 libxml 之间存在一些脱节,导致它无法加载 SYSTEM DTD,或识别 PUBLIC DTD.如果您在 XML 文件中定义 DTD,它工作,但祝您在 XHTML DTD 中这样做好运.

我能推荐的最好的事情是使用 XHTML 模式相反:

需要'nokogiri'需要'open-uri'doc = Nokogiri::XML(open('http://www.w3.org'))xsd = Nokogiri::XML::Schema(open('http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd'))#这是一个真/假验证xsd.valid?(doc) # =>真的#this 给出错误列表xsd.validate(doc) # =>[]

I've found a few posts alluding to the fact that you can validate XHTML against its DTD using the nokogiri gem. Whilst I've managed to use it to parse XHTML successfully (looking for 'a' tags etc.), I'm struggling to validate documents.

For me, this:

doc = Nokogiri::XML(Net::HTTP.get(URI.parse("http://www.w3.org")))
puts doc.validate

results in a whole heap of:

[
#<Nokogiri::XML::SyntaxError: No declaration for element html>,
#<Nokogiri::XML::SyntaxError: No declaration for attribute xmlns of element html>,
#<Nokogiri::XML::SyntaxError: No declaration for attribute lang of element html>,  
#<Nokogiri::XML::SyntaxError: No declaration for attribute lang of element html>,
#<Nokogiri::XML::SyntaxError: No declaration for element head>,
#<Nokogiri::XML::SyntaxError: No declaration for attribute profile of element head
[repeat for every tag in the document.]
]

So I'm assuming that's not the right approach. I can't seem to locate any good examples -- can anyone suggest what I'm doing wrong?

I'm running ruby 1.8.6 on Mac OSX 10.5.8. Nokogiri tells me:

nokogiri: 1.3.3
warnings: []

libxml: 
  compiled: 2.6.23
  loaded: 2.6.23
  binding: extension

解决方案

It's not just you. What you're doing is supposed to be the right way to do it, but I've never had any luck with it. As far as I can tell, there's some disconnect somewhere between Nokogiri and libxml which causes it to not load SYSTEM DTDs, or to recognize PUBLIC DTDs. It will work if you define the DTD within the XML file, but good luck doing that with the XHTML DTDs.

The best thing I can recommend is to use the schemas for XHTML instead:

require 'nokogiri'
require 'open-uri'

doc = Nokogiri::XML(open('http://www.w3.org'))
xsd = Nokogiri::XML::Schema(open('http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd'))

#this is a true/false validation
xsd.valid?(doc)    # => true

#this gives a listing of errors
xsd.validate(doc)  # => []

这篇关于如何使用 nokogiri 验证 XHTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆