Class: Nokogiri::XML::RelaxNG
- Inherits:
-
NokogiriXmlSchema
- Object
- NokogiriXmlSchema
- Nokogiri::XML::RelaxNG
- 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.
end
The list of errors are Nokogiri::XML::SyntaxError objects.
Class Method Summary collapse
-
.from_document(doc) ⇒ Object
Create a new RelaxNG schema from the Nokogiri::XML::Document
doc
. -
.read_memory(string) ⇒ Object
Create a new RelaxNG from the contents of
string
.
Class Method Details
.from_document(doc) ⇒ Object
Create a new RelaxNG schema from the Nokogiri::XML::Document doc
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'ext/nokogiri/xml_relax_ng.c', line 103
static VALUE from_document(VALUE klass, VALUE document)
{
xmlDocPtr doc;
xmlRelaxNGParserCtxtPtr ctx;
xmlRelaxNGPtr schema;
VALUE errors;
VALUE rb_schema;
Data_Get_Struct(document, xmlDoc, doc);
/* In case someone passes us a node. ugh. */
doc = doc->doc;
ctx = xmlRelaxNGNewDocParserCtxt(doc);
errors = rb_ary_new();
xmlSetStructuredErrorFunc((void *)errors, Nokogiri_error_array_pusher);
#ifdef HAVE_XMLRELAXNGSETPARSERSTRUCTUREDERRORS
xmlRelaxNGSetParserStructuredErrors(
ctx,
Nokogiri_error_array_pusher,
(void *)errors
);
#endif
schema = xmlRelaxNGParse(ctx);
xmlSetStructuredErrorFunc(NULL, NULL);
if(NULL == schema) {
xmlErrorPtr error = xmlGetLastError();
if(error)
Nokogiri_error_raise(NULL, error);
else
rb_raise(rb_eRuntimeError, "Could not parse document");
return Qnil;
}
rb_schema = Data_Wrap_Struct(klass, 0, dealloc, schema);
rb_iv_set(rb_schema, "@errors", errors);
return rb_schema;
}
|
.read_memory(string) ⇒ Object
Create a new RelaxNG from the contents of string
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'ext/nokogiri/xml_relax_ng.c', line 56
static VALUE read_memory(VALUE klass, VALUE content)
{
xmlRelaxNGParserCtxtPtr ctx = xmlRelaxNGNewMemParserCtxt(
(const char *)StringValuePtr(content),
(int)RSTRING_LEN(content)
);
xmlRelaxNGPtr schema;
VALUE errors = rb_ary_new();
VALUE rb_schema;
xmlSetStructuredErrorFunc((void *)errors, Nokogiri_error_array_pusher);
#ifdef HAVE_XMLRELAXNGSETPARSERSTRUCTUREDERRORS
xmlRelaxNGSetParserStructuredErrors(
ctx,
Nokogiri_error_array_pusher,
(void *)errors
);
#endif
schema = xmlRelaxNGParse(ctx);
xmlSetStructuredErrorFunc(NULL, NULL);
xmlRelaxNGFreeParserCtxt(ctx);
if(NULL == schema) {
xmlErrorPtr error = xmlGetLastError();
if(error)
Nokogiri_error_raise(NULL, error);
else
rb_raise(rb_eRuntimeError, "Could not parse document");
return Qnil;
}
rb_schema = Data_Wrap_Struct(klass, 0, dealloc, schema);
rb_iv_set(rb_schema, "@errors", errors);
return rb_schema;
}
|