7

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

2 Answers 2

20

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
2
  • dead link ... can it be removed? Commented Jan 7, 2022 at 11:22
  • 1
    Thanks for noting it, David. Just removed.
    – LuisVM
    Commented Jan 8, 2022 at 16:25
17

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.

2
  • 1
    At first I was thinking: "wait, this cannot work". Turns out it works great :) Thank you @ernestothorp
    – radiospiel
    Commented Jun 3, 2020 at 12:57
  • Just ran into the same issue and it worked! Just use File.open instead of File.read and you're fine.
    – Tintin81
    Commented Jun 5 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.