validate_document (Nokogiri::XML::RelaxNG)

    /*
     * call-seq:
     *  validate_document(document)
     *
     * Validate a Nokogiri::XML::Document against this RelaxNG schema.
     */
    static VALUE validate_document(VALUE self, VALUE document)
    {
      xmlDocPtr doc;
      xmlRelaxNGPtr schema;

      Data_Get_Struct(self, xmlRelaxNG, schema);
      Data_Get_Struct(document, xmlDoc, doc);

      VALUE errors = rb_ary_new();

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