解决方法
好吧,我有一个程序构建但结果不好.
我的结论如下:
> rexml relaxng模式解析可能不起作用.代码说明它不完整
> rexml pull解析可能有效,但很难说
>上述两种都没有记录
>您应该使用真正的XML库,例如libxml
这是我的测试程序:test.rb
require 'rexml/validation/relaxng.rb' require 'rexml/parsers/pullparser.rb' # USAGE: ruby test.rb XML-FILE xml = ARGV[0] # schema must be a Relax NG XML (NOT compact / .rnc) schema = File.new( "example.rng" ) validator = REXML::Validation::RelaxNG.new( schema ) # The structure the validator made,which should be a complex structure but isn't validator.dump xmlfile = File.new( xml ) parser = REXML::Parsers::PullParser.new( xmlfile ) while parser.has_next? # Returns an PullEvent e = parser.pull # puts "Event ",e.inspect validator.validate(e) end
我制作了一些玩具示例XML文件和RNG文件,然后在OSX 10.5.x上试用了它
(打破长线以使其可读):
$/usr/bin/ruby test.rb good.xml < S.1 #{doc},:end_document( ) > /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rexml/ validation/validation.rb:24:in `validate': Validation error. Expected: :start_element( doc ) from < S.1 #:start_element( doc ),{head},{body},:end_element( ),:end_document( ) > but got "doc"( ) (REXML::Validation::ValidationException) from test.rb:20
(我和1.9一样)
所以,几乎失败了.
(我可以更多地优化测试程序以使用add_listener,但它似乎不值得)