Class: Nokogiri::XML::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/nokogiri/xml/schema.rb,
ext/nokogiri/xml_schema.c

Overview

Nokogiri::XML::Schema is used for validating XML against a schema (usually from an xsd file).

Synopsis

Validate an XML document against a Schema. Loop over the errors that are returned and print them out:

xsd = Nokogiri::XML::Schema(File.read(PO_SCHEMA_FILE))
doc = Nokogiri::XML(File.read(PO_XML_FILE))

xsd.validate(doc).each do |error|
  puts error.message
end

The list of errors are Nokogiri::XML::SyntaxError objects.

NOTE: As of v1.11.0, Schema treats inputs as UNTRUSTED by default, and so external entities are not resolved from the network (‘http://` or `ftp://`). Previously, parsing treated documents as “trusted” by default which was counter to Nokogiri’s “untrusted by default” security policy. If a document is trusted, then the caller may turn off the NONET option via the ParseOptions to re-enable external entity resolution over a network connection.

Direct Known Subclasses

RelaxNG

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject

Errors while parsing the schema file



39
40
41
# File 'lib/nokogiri/xml/schema.rb', line 39

def errors
  @errors
end

#parse_optionsObject

The Nokogiri::XML::ParseOptions used to parse the schema



41
42
43
# File 'lib/nokogiri/xml/schema.rb', line 41

def parse_options
  @parse_options
end

Class Method Details

.from_document(document) ⇒ Object

Create a new schema parsed from the document.

Parameters
  • document: Nokogiri::XML::Document to be parsed

Returns

Nokogiri::XML::Schema



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'ext/nokogiri/xml_schema.c', line 205

static VALUE
rb_xml_schema_s_from_document(int argc, VALUE *argv, VALUE klass)
{
  VALUE rb_document;
  VALUE rb_parse_options;
  VALUE rb_schema;
  xmlDocPtr c_document;
  xmlSchemaParserCtxtPtr c_parser_context;
  int defensive_copy_p = 0;

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

  if (!rb_obj_is_kind_of(rb_document, cNokogiriXmlNode)) {
    rb_raise(rb_eTypeError,
             "expected parameter to be a Nokogiri::XML::Document, received %"PRIsVALUE,
             rb_obj_class(rb_document));
  }

  if (!rb_obj_is_kind_of(rb_document, cNokogiriXmlDocument)) {
    xmlNodePtr deprecated_node_type_arg;
    // TODO: deprecate allowing Node
    NOKO_WARN_DEPRECATION("Passing a Node as the first parameter to Schema.from_document is deprecated. Please pass a Document instead. This will become an error in a future release of Nokogiri.");
    Noko_Node_Get_Struct(rb_document, xmlNode, deprecated_node_type_arg);
    c_document = deprecated_node_type_arg->doc;
  } else {
    c_document = noko_xml_document_unwrap(rb_document);
  }

  if (noko_xml_document_has_wrapped_blank_nodes_p(c_document)) {
    // see https://github.com/sparklemotion/nokogiri/pull/2001
    c_document = xmlCopyDoc(c_document, 1);
    defensive_copy_p = 1;
  }

  c_parser_context = xmlSchemaNewDocParserCtxt(c_document);
  rb_schema = xml_schema_parse_schema(klass, c_parser_context, rb_parse_options);

  if (defensive_copy_p) {
    xmlFreeDoc(c_document);
    c_document = NULL;
  }

  return rb_schema;
}

.new(string_or_io, options = ParseOptions::DEFAULT_SCHEMA) ⇒ Object

Create a new Nokogiri::XML::Schema object using a string_or_io object.



46
47
48
# File 'lib/nokogiri/xml/schema.rb', line 46

def self.new(string_or_io, options = ParseOptions::DEFAULT_SCHEMA)
  from_document(Nokogiri::XML(string_or_io), options)
end

.read_memory(string) ⇒ Object

Create a new schema parsed from the contents of string

Parameters
  • string: String containing XML to be parsed as a schema

Returns

Nokogiri::XML::Schema



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'ext/nokogiri/xml_schema.c', line 177

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

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

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

  return xml_schema_parse_schema(klass, c_parser_context, rb_parse_options);
}

Instance Method Details

#valid?(thing) ⇒ Boolean

Returns true if thing is a valid Nokogiri::XML::Document or file.

Returns:

  • (Boolean)


68
69
70
# File 'lib/nokogiri/xml/schema.rb', line 68

def valid?(thing)
  validate(thing).empty?
end

#validate(thing) ⇒ Object

Validate thing against this schema. thing can be a Nokogiri::XML::Document object, or a filename. An Array of Nokogiri::XML::SyntaxError objects found while validating the thing is returned.



55
56
57
58
59
60
61
62
63
# File 'lib/nokogiri/xml/schema.rb', line 55

def validate(thing)
  if thing.is_a?(Nokogiri::XML::Document)
    validate_document(thing)
  elsif File.file?(thing)
    validate_file(thing)
  else
    raise ArgumentError, "Must provide Nokogiri::Xml::Document or the name of an existing file"
  end
end