Class: Nokogiri::XML::RelaxNG

Inherits:
Schema
  • Object
show all
Defined in:
lib/nokogiri/xml/relax_ng.rb,
ext/nokogiri/xml_relax_ng.c

Overview

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

Example: Determine whether an 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 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.

Instance Attribute Summary

Attributes inherited from Schema

#errors, #parse_options

Class Method Summary collapse

Methods inherited from Schema

#valid?, #validate

Class Method Details

.from_document(*args) ⇒ Object

:call-seq:

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 XML::Document object representing the parsed RELAX NG

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

Returns

Nokogiri::XML::RelaxNG

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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);
}

.new(input, parse_options = ParseOptions::DEFAULT_SCHEMA) ⇒ Object

:call-seq:

new(input) → Nokogiri::XML::RelaxNG
new(input, parse_options) → Nokogiri::XML::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

Nokogiri::XML::RelaxNG

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



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

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

.read_memory(input, parse_options = ParseOptions::DEFAULT_SCHEMA) ⇒ Object

:call-seq:

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

Nokogiri::XML::RelaxNG

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



80
81
82
# 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