RELAX NG schema validation in Ruby using the Jing CLI
= Ruby Jing
{}[https://github.com/sshaw/ruby-jing/actions/workflows/rake.yml] {}[https://codeclimate.com/github/sshaw/ruby-jing]
RELAX NG schema validation using the {Jing CLI}[http://www.thaiopensource.com/relaxng/jing.html]
=== Overview
require "jing"
jing = Jing.new("schema.rng") begin errors = jing.validate("doc.xml") rescue Jing::Error => e abort "what what what #{e}" end
if errors.none? puts "Valid!" else errors.each do |error| puts "#{error[:message]} @ #{error[:line]}:#{error[:column]}" end end
abort "Invalid!" unless jing.valid?("/path/to/doc.xml")
=== Why use Java to validate instead of Ruby libraries like Nokorigi, REXML, libxml, etc..?
Simple: good error messages. Let's look at the error messages provided by each of these libraries.
==== Nokorigi/libxml
schema = Nokogiri::XML::RelaxNG(File.read(rng)) doc = Nokogiri::XML(File.read(xml)) errors = schema.validate(doc) errors.each { |e| puts e }
Resulting errors:
Element card failed to validate attributes Expecting an element , got nothing
Element card failed to validate attributes Did not expect element oops there
==== REXML
include REXML doc = Document.new(File.read(xml)) validator = Validation::RelaxNG.new(File.read(rng)) validator.validate(doc)
Fails for XML A and XML B --it treats the XML declaration as a validation error!
Validation error. Expected: :start_element( addressBook ) from < S.1 #:start_element( addressBook ), < Z.2 #:start_element( card ), :start_attribute( version ), < C.3 :text( v1 ) or :text( v2 ) >, :end_attribute( ), :start_element( name ), :text( ), :end_element( ), :start_element( email ), :text( ), :end_element( ), :end_element( ) >, :end_element( ), :end_document( ) > but got ( )
==== Jing
jing = Jing.new(schema) errors = jing.validate(xml) errors.each { |e| puts e[:message] }
Resulting errors:
element "card" missing required attribute "version" element "card" incomplete; missing required element "name"
value of attribute "version" is invalid; must be equal to "v1" or "v2" element "oops" not allowed anywhere; expected the element end-tag
Better, don't ya think?
=== More Info
=== Author
Skye Shaw [skye.shaw AT gmail.com]
=== License
Released under the MIT License: www.opensource.org/licenses/MIT
=== Jing Copying Conditions
Copyright (c) 2001-2003 Thai Open Source Software Center Ltd All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.