Bene, ho creato un programma ma i risultati non sono buoni.
Le mie conclusioni sono le seguenti:
- rexml schema RelaxNG analisi probabilmente non funziona. il codice note è incompleto
- rexml tirare parsing probabilmente funziona, ma difficile dire
- sia di quanto sopra sono senza documenti
- si dovrebbe usare una libreria XML vero e proprio, come libxml
Ecco il mio programma di test : 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
e ho fatto alcuni file XML giocattolo di esempio e file RNG e poi deciso di provarlo su OSX 10.5.x 012.(lunga linea spezzata per renderlo leggibile):
$ /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
(ottengo lo stesso con 1.9)
Così, più o meno il fallimento.
(avrei potuto ottimizzato il programma di test un po 'di più per utilizzare add_listener ma non sembrava vale la pena)
+1 per provare! :-) – cdleary