class Nokogiri::XML::RelaxNG

Nokogiri::XML::RelaxNG is used for validating XML against a RELAX NG schema definition.

🛡 Do not use this class for untrusted schema documents. RELAX NG input is always treated as trusted, meaning that the underlying parsing libraries will access network resources. This is counter to Nokogiri’s “untrusted by default” security policy, but is an unfortunate limitation of the underlying libraries.

Example: Determine whether an XML document is valid.

schema = Nokogiri::XML::RelaxNG.new(File.read(RELAX_NG_FILE))
doc = Nokogiri::XML::Document.parse(File.read(XML_FILE))
schema.valid?(doc) # Boolean

Example: Validate an XML document against a RelaxNG schema, and capture any errors that are found.

schema = Nokogiri::XML::RelaxNG.new(File.open(RELAX_NG_FILE))
doc = Nokogiri::XML::Document.parse(File.open(XML_FILE))
errors = schema.validate(doc) # Array<SyntaxError>

Example: Validate an XML document using a Document containing a RELAX NG schema definition.

schema_doc = Nokogiri::XML::Document.parse(File.read(RELAX_NG_FILE))
schema = Nokogiri::XML::RelaxNG.from_document(schema_doc)
doc = Nokogiri::XML::Document.parse(File.open(XML_FILE))
schema.valid?(doc) # Boolean