The REXML
module appears to have support for RELAX NG validation, but the docs don't have any real information on using the validation portion of the framework.
How would you validate an XML document with a RELAX NG schema? A code snippet would be most helpful. TIA!
Well, I got a program constructed but the results aren't good.
My conclusions are as follows:
Here's my test program: 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
and I made some toy example XML files and RNG files and then tried it out on OSX 10.5.x (long line broken to make it readable):
$ /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
(I get the same with 1.9)
So, pretty much failure.
(I could have optimized the test program some more to use add_listener but it didn't seem worthwhile)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With