Resolving validation errors using Nokogiri and schemas

A note to my future self:

Scenario: validating a document using Nokogiri and a xsd which references other schemas.

Error: Something like:

schema.rb:37:in `from_document': attribute use (unknown), attribute 'ref': The QName value '{http://url.of.the.referenced.schema}blah' does not resolve to a(n) attribute declaration. (Nokogiri::XML::SyntaxError)

or the referenced schemas are using a URL to somewhere out there and Nokogiri tries to download it, in which case the script is slowed down or it throws a download error.

Solution: If you reference remote schemas, download them and put them all together in a single directory. If you already have the xsd files in your machine, just put them together in the same directory. Then change your xsd to use a relative path. For example:

Change this

<xs:import namespace=""


<xs:import namespace=""


Then wrap the validation code inside a Dir.chdir call. Like this:

Dir.chdir(somewhere) do
schema = Nokogiri::XML::Schema('your-schema.xsd'))
doc = Nokogiri::XML(

Source of enlightment: A bug on JRuby’s version of nokogiri

Categorías: Investigacion

Leave a comment