123456789_123456789_123456789_123456789_123456789_

Class: Nokogiri::XML::RelaxNG

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Schema
Instance Chain:
self, Schema
Inherits: Nokogiri::XML::Schema
Defined in: lib/nokogiri/xml/relax_ng.rb,
ext/nokogiri/xml_relax_ng.c

Overview

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

Example: Determine whether an ::Nokogiri::XML document is valid.

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

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

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

⚠ 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. Please do not use this class for untrusted schema documents.

Class Method Summary

Schema - Inherited

.from_document

Create a Schema from an already-parsed ::XSD schema definition document.

.new

Parse an ::XSD schema definition and create a new Nokogiri::XML:Schema object.

.read_memory

Parse an ::XSD schema definition and create a new Schema object.

Instance Attribute Summary

Schema - Inherited

#errors

The errors found while parsing the ::XSD.

#parse_options

The options used to parse the schema.

Instance Method Summary

Schema - Inherited

#valid?

Validate input and return a Boolean indicating whether the document is valid.

#validate

Validate input and return any errors that are found.

#validate_document, #validate_file

Constructor Details

.new(input) → Nokogiri::XML::RelaxNG) ⇒ RelaxNG .new(input, parse_options) → Nokogiri::XML::RelaxNG) ⇒ RelaxNG

Parse a RELAX NG schema definition and create a new Schema object.

Parameters
  • input (String, IO) RELAX NG schema definition

  • parse_options (Nokogiri::XML::ParseOptions) Defaults to ParseOptions::DEFAULT_SCHEMA ⚠ Unused

Returns

RelaxNG

parse_options is currently unused by this method and is present only as a placeholder for future functionality.

[ GitHub ]

  
# File 'lib/nokogiri/xml/relax_ng.rb', line 61

def self.new(input, parse_options = ParseOptions::DEFAULT_SCHEMA)
  read_memory(input, parse_options)
end

Class Method Details

.from_document(document) → Nokogiri::XML::RelaxNG) .from_document(document, parse_options) → Nokogiri::XML::RelaxNG)

Create a Schema from an already-parsed RELAX NG schema definition document.

Parameters
  • document (XML::Document) A Document object representing the parsed RELAX NG

  • parse_options (Nokogiri::XML::ParseOptions) ⚠ Unused

Returns

RelaxNG

parse_options is currently unused by this method and is present only as a placeholder for future functionality.

[ GitHub ]

  
# File 'ext/nokogiri/xml_relax_ng.c', line 119

static VALUE
noko_xml_relax_ng_s_from_document(int argc, VALUE *argv, VALUE rb_class)
{
  VALUE rb_document;
  VALUE rb_parse_options;
  xmlDocPtr c_document;
  xmlRelaxNGParserCtxtPtr c_parser_context;

  rb_scan_args(argc, argv, "11", &rb_document, &rb_parse_options);

  c_document = noko_xml_document_unwrap(rb_document);
  c_document = c_document->doc; /* In case someone passes us a node. ugh. */

  c_parser_context = xmlRelaxNGNewDocParserCtxt(c_document);

  return xml_relax_ng_parse_schema(rb_class, c_parser_context, rb_parse_options);
}

.read_memory(input) → Nokogiri::XML::RelaxNG) .read_memory(input, parse_options) → Nokogiri::XML::RelaxNG)

Parse a RELAX NG schema definition and create a new Schema object.

Parameters
  • input (String) RELAX NG schema definition

  • parse_options (Nokogiri::XML::ParseOptions) Defaults to ParseOptions::DEFAULT_SCHEMA ⚠ Unused

Returns

RelaxNG

parse_options is currently unused by this method and is present only as a placeholder for future functionality.

[ GitHub ]

  
# File 'lib/nokogiri/xml/relax_ng.rb', line 80

def self.read_memory(input, parse_options = ParseOptions::DEFAULT_SCHEMA)
  from_document(Nokogiri::XML::Document.parse(input), parse_options)
end

Instance Method Details

#validate_document(document) (private)

[ GitHub ]

  
# File 'ext/nokogiri/xml_relax_ng.c', line 20

static VALUE
noko_xml_relax_ng__validate_document(VALUE self, VALUE document)
{
  xmlDocPtr doc;
  xmlRelaxNGPtr schema;
  VALUE errors;
  xmlRelaxNGValidCtxtPtr valid_ctxt;

  TypedData_Get_Struct(self, xmlRelaxNG, &xml_relax_ng_type, schema);
  doc = noko_xml_document_unwrap(document);

  errors = rb_ary_new();

  valid_ctxt = xmlRelaxNGNewValidCtxt(schema);

  if (NULL == valid_ctxt) {
    /* we have a problem */
    rb_raise(rb_eRuntimeError, "Could not create a validation context");
  }

  xmlRelaxNGSetValidStructuredErrors(
    valid_ctxt,
    noko__error_array_pusher,
    (void *)errors
  );

  xmlRelaxNGValidateDoc(valid_ctxt, doc);

  xmlRelaxNGFreeValidCtxt(valid_ctxt);

  return errors;
}