Class: Nokogiri::XML::Schema
- Inherits:
-
Object
- Object
- Nokogiri::XML::Schema
- 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.
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
Instance Attribute Summary collapse
-
#errors ⇒ Object
Errors while parsing the schema file.
-
#parse_options ⇒ Object
The Nokogiri::XML::ParseOptions used to parse the schema.
Class Method Summary collapse
-
.from_document(document) ⇒ Object
Create a new schema parsed from the
document
. -
.new(string_or_io, options = ParseOptions::DEFAULT_SCHEMA) ⇒ Object
Create a new Nokogiri::XML::Schema object using a
string_or_io
object. -
.read_memory(string) ⇒ Object
Create a new schema parsed from the contents of
string
.
Instance Method Summary collapse
-
#valid?(thing) ⇒ Boolean
Returns true if
thing
is a valid Nokogiri::XML::Document or file. -
#validate(thing) ⇒ Object
Validate
thing
against this schema.
Instance Attribute Details
#errors ⇒ Object
Errors while parsing the schema file
39 40 41 |
# File 'lib/nokogiri/xml/schema.rb', line 39 def errors @errors end |
#parse_options ⇒ Object
The Nokogiri::XML::ParseOptions used to parse the schema
41 42 43 |
# File 'lib/nokogiri/xml/schema.rb', line 41 def @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 |
# File 'ext/nokogiri/xml_schema.c', line 205
static VALUE
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, cNokogiriXmlDocument)) {
c_document = noko_xml_document_unwrap(rb_document);
} else {
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;
}
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, = ParseOptions::DEFAULT_SCHEMA) from_document(Nokogiri::XML(string_or_io), ) 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.
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 |