Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 462
LuisVM Avatar asked Aug 16 '12 22:08

LuisVM


2 Answers

If all your .xsd files are in the same place, and all namespaces are declared like

<import namespace="urn:...:ns:name-1.0"
      schemaLocation="name-1.0.xsd"/>

just use File.open instead of File.read to read the .xsd file

Nokogiri will handle to open all depending schemas.

like image 186
ernestothorp Avatar answered Oct 19 '22 13:10

ernestothorp


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
like image 45
LuisVM Avatar answered Oct 19 '22 15:10

LuisVM