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 XML against a RELAX NG schema definition.
🛡 Do not use this class for untrusted schema documents. 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.
Example: Determine whether an XML document is valid.
schema = Nokogiri::XML::RelaxNG.new(File.read(RELAX_NG_FILE))
doc = Nokogiri::XML::Document.parse(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.new(File.open(RELAX_NG_FILE))
doc = Nokogiri::XML::Document.parse(File.open(XML_FILE))
errors = schema.validate(doc) # Array<SyntaxError>
Example: Validate an XML document using a Document
containing a RELAX NG schema definition.
schema_doc = Nokogiri::XML::Document.parse(File.read(RELAX_NG_FILE))
schema = Nokogiri::XML::RelaxNG.from_document(schema_doc)
doc = Nokogiri::XML::Document.parse(File.open(XML_FILE))
schema.valid?(doc) # Boolean
Class Method Summary
-
.from_document(document) → Nokogiri::XML::RelaxNG)
Parse a RELAX NG schema definition from a
Document
to create a newRelaxNG
. -
.new(input) → Nokogiri::XML::RelaxNG) ⇒ RelaxNG
constructor
Parse a RELAX NG schema definition from a String or IO to create a new
RelaxNG
. -
.read_memory(input) → Nokogiri::XML::RelaxNG)
Convenience method for .new.
Schema
- Inherited
.from_document | |
.new | Parse an XSD schema definition from a String or IO to create a new |
.read_memory | Convenience method for Schema.new |
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
- #validate_document(document) private
Schema
- Inherited
#valid? | Validate |
#validate | Validate |
#validate_document, #validate_file |
Constructor Details
.new(input) → Nokogiri::XML::RelaxNG) ⇒ RelaxNG
.new(input, options:) → Nokogiri::XML::RelaxNG) ⇒ RelaxNG
RelaxNG
.new(input, options:) → Nokogiri::XML::RelaxNG) ⇒ RelaxNG
Parse a RELAX NG schema definition from a String or IO to create a new RelaxNG
.
- Parameters
-
input
(String | IO) RELAX NG schema definition -
options:
(Nokogiri::XML::ParseOptions) Defaults to Nokogiri::XML::ParseOptions::DEFAULT_SCHEMA ⚠ Unused
- Returns
-
RelaxNG
⚠ parse_options
is currently unused by this method and is present only as a placeholder for future functionality.
Also see convenience method Nokogiri::XML::RelaxNG()
# File 'lib/nokogiri/xml/relax_ng.rb', line 60
def self.new(input, = ParseOptions::DEFAULT_SCHEMA, options: ) from_document(Nokogiri::XML::Document.parse(input), ) end
Class Method Details
.from_document(document) → Nokogiri::XML::RelaxNG)
.from_document(document, parse_options) → Nokogiri::XML::RelaxNG)
Parse a RELAX NG schema definition from a Document
to create a new RelaxNG
.
- Parameters
-
document
(XML::Document) A document containing the 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.
# File 'ext/nokogiri/xml_relax_ng.c', line 120
static VALUE noko_xml_relax_ng_s_from_document(int argc, VALUE *argv, VALUE rb_class) { /* TODO: deprecate this method and put file-or-string logic into .new so that becomes the * preferred entry point, and this can become a private method */ 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 _noko_xml_relax_ng_parse_schema(rb_class, c_parser_context, rb_parse_options); }
.read_memory(input) → Nokogiri::XML::RelaxNG)
.read_memory(input, options:) → Nokogiri::XML::RelaxNG)
Convenience method for .new.
# File 'lib/nokogiri/xml/relax_ng.rb', line 69
def self.read_memory(...) # TODO deprecate this method new(...) 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; }