Read more

Validate an XML document against an XSD schema with Ruby and Nokogiri

Henning Koch
February 17, 2011Software engineer at makandra GmbH

The code below shows a method #validate which uses Nokogiri to validate an XML document against an XSD schema. It returns an array of Nokogiri::XML::SyntaxError Show archive.org snapshot objects.

require 'rubygems'
gem 'nokogiri'
require 'nokogiri'

def validate(document_path, schema_path, root_element)
  schema = Nokogiri::XML::Schema(File.read(schema_path))
  document = Nokogiri::XML(File.read(document_path))
  schema.validate(document.xpath("//#{root_element}").to_s)
end

validate('input.xml', 'schema.xdf', 'container').each do |error|
  puts error.message
end
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

This note was contributed by Matthias Marschall from the Agile Web Development & Operations Show archive.org snapshot blog.

Posted by Henning Koch to makandra dev (2011-02-17 14:03)