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 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.

Instance Attribute Summary

Attributes inherited from Schema

#errors, #parse_options

Class Method Summary collapse

Methods inherited from Schema

new, #valid?, #validate

Class Method Details

.from_document(doc) ⇒ Object

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# 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) ⇒ Object

Create a new RelaxNG from the contents of string



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'ext/nokogiri/xml_relax_ng.c', line 119

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