Search code examples
rubyxsdnokogirisaml

Nokogiri::XML::Schema SyntaxError on schema load


I'm trying to load a SAML Protocol schema (specificly this: https://www.oasis-open.org/committees/download.php/3407/oasis-sstc-saml-schema-protocol-1.1.xsd) but after doing this:

schema = Nokogiri::XML::Schema(File.read('saml11_schema.xsd'))

I'm getting this output:

Nokogiri::XML::SyntaxError Exception: Element '{http://www.w3.org/2001/XMLSchema}element', attribute 'ref': The QName value '{urn:oasis:names:tc:SAML:1.0:assertion}Assertion' does not resolve to a(n) element declaration.

Tried googling the error but there are no clues on what might be happening, could someone shed some light?

Note: Using RVM with Ruby 1.8.7-p370


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="http://www.w3.org/XML/1998/namespace"
    schemaLocation="http://www.w3.org/2001/xml.xsd"/>
    

    to

    <xs:import namespace="http://www.w3.org/XML/1998/namespace"
    schemaLocation="xml.xsd"/>
    

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

    Dir.chdir(somewhere) do
    schema = Nokogiri::XML::Schema(IO.read('your-schema.xsd'))
    doc = Nokogiri::XML(IO.read(doc_path))
    schema.validate(doc)
    end