class Nokogiri::XML::RelaxNG - nokogiri-1.15.2 Documentation

# class Nokogiri::XML::RelaxNG

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

## Synopsis¶ ↑

Validate an `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 `Nokogiri::XML::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.

### Public Class Methods

from_document(doc) click to toggle source

Create a new `RelaxNG` schema from the `Nokogiri::XML::Document` `doc`

    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) click to toggle source

Create a new `RelaxNG` from the contents of `string`

    static VALUE
    read_memory(int argc, VALUE *argv, VALUE klass)
    {
      VALUE rb_content;
      VALUE rb_parse_options;
      xmlRelaxNGParserCtxtPtr c_parser_context;

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

      c_parser_context = xmlRelaxNGNewMemParserCtxt(
                           (const char *)StringValuePtr(rb_content),
                           (int)RSTRING_LEN(rb_content)
                         );

      return xml_relax_ng_parse_schema(klass, c_parser_context, rb_parse_options);
    }

### Private Instance Methods

validate_document(document) click to toggle source

Validate a `Nokogiri::XML::Document` against this `RelaxNG` schema.

    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;
    }