如何使用Nokogiri使用本地dtd文件正确validationxml文件?

我有一个简单,有效的DTD和一个似乎符合DTD的有效XML文件,但Nokogiri正在生成大量validation输出,这意味着XML文件未通过validation。

dtd文件是:

                               

xml文件是:

     FOO SOFTWARE.    The core global object. This is a special singleton object. It is used for internal Wayland protocol features.    The sync request asks the server to emit the 'done' event on the returned wl_callback object. Since requests are handled in-order and events are delivered in-order, this can be used as a barrier to ensure all previous requests and the resulting events have been handled. The object returned by this request will be destroyed by the compositor after the callback is fired and as such the client must not attempt to use it after that point. The callback_data passed in the callback is the event serial.      

我简单的Ruby程序是:

 require 'nokogiri' DTD_PATH = "wayland.dtd" XML_PATH = "wayland.xml" dtd_doc = Nokogiri::XML::Document.parse(open(DTD_PATH)) dtd = Nokogiri::XML::DTD.new('protocol', dtd_doc) doc = Nokogiri::XML(open(XML_PATH)) puts dtd.validate(doc) 

程序打印validation数组的内容,该数组不为空。 样本输出:

 No declaration for attribute name of element request No declaration for element description No declaration for attribute summary of element description 

即使在向xml文件添加DOCTYPE声明之后,也是如此:

  

并包装DTD:

  

我仍然观察到同样失败的validation输出。 我究竟做错了什么?

您可以通过指定ParseOptions进行validation。 您需要使用doctype声明指定doctype

 require 'nokogiri' DTD_PATH = "wayland.dtd" XML_PATH = "wayland.xml" xml = File.read(XML_PATH) options = Nokogiri::XML::ParseOptions::DTDVALID doc = Nokogiri::XML::Document.parse(xml, nil, nil, options) puts doc.external_subset.validate(doc)