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,
ext/nokogiri/xml_schema.c

Overview

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

Synopsis

Validate an ::Nokogiri::XML document against a RelaxNG schema. Loop over the errors that are returned and print them out:

schema  = Nokogiri::XML::RelaxNG(File.open(ADDRESS_SCHEMA_FILE))
doc     = Nokogiri::XML(File.open(ADDRESS_XML_FILE))

schema.validate(doc).each do |error|
  puts error.message
end

The list of errors are SyntaxError objects.

NOTE: RelaxNG input is always treated as TRUSTED documents, meaning that they will cause the underlying parsing libraries to access network resources. This is counter to Nokogiri’s “untrusted by default” security policy, but is a limitation of the underlying libraries.

Class Method Summary

Schema - Inherited

.from_document

Create a new schema parsed from the document.

.new

Create a new Schema object using a string_or_io object.

.read_memory

Create a new schema parsed from the contents of string

Instance Attribute Summary

Schema - Inherited

#errors

Errors while parsing the schema file.

#parse_options

The ParseOptions used to parse the schema.

Instance Method Summary

Schema - Inherited

#valid?

Returns true if thing is a valid Document or file.

#validate

Validate thing against this schema.

#validate_document,
#validate_file

Validate a file against this Schema.

Constructor Details

This class inherits a constructor from Nokogiri::XML::Schema

Class Method Details

.from_document(doc)

Create a new RelaxNG schema from the Document doc

[ GitHub ]

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

static VALUE
from_document(int argc, VALUE *argv, VALUE klass)
{
  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(klass, c_parser_context, rb_parse_options);
}

.read_memory(string) → Nokogiri::XML::Schema)

Alias for Document.read_memory. Create a new schema parsed from the contents of string

Parameters
Returns

Schema

Instance Method Details

#validate_document(document) (private)

[ GitHub ]

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

static VALUE
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");
  }

#ifdef HAVE_XMLRELAXNGSETVALIDSTRUCTUREDERRORS
  xmlRelaxNGSetValidStructuredErrors(
    valid_ctxt,
    Nokogiri_error_array_pusher,
    (void *)errors
  );
#endif

  xmlRelaxNGValidateDoc(valid_ctxt, doc);

  xmlRelaxNGFreeValidCtxt(valid_ctxt);

  return errors;
}