класс Nokogiri::XML::RelaxNG
Nokogiri::XML::RelaxNG используется для проверки XML по схеме RelaxNG.
Краткое описание
Проверить документ XML по схеме RelaxNG. Перебрать ошибки и вывести их:
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
Список ошибок — объекты Nokogiri::XML::SyntaxError.
ПРИМЕЧАНИЕ: Ввод RelaxNG всегда обрабатывается как доверенный документ, что означает, что он может заставить библиотеки парсинга обращаться к сетевым ресурсам. Это противоречит политике безопасности Nokogiri «по умолчанию недоверенный», но является ограничением базовых библиотек.
Открытые методы класса
static VALUE
from_document(int argc, VALUE *argv, VALUE klass)
{
VALUE document;
VALUE parse_options;
xmlDocPtr doc;
xmlRelaxNGParserCtxtPtr ctx;
xmlRelaxNGPtr schema;
VALUE errors;
VALUE rb_schema;
int scanned_args = 0;
scanned_args = rb_scan_args(argc, argv, "11", &document, &parse_options);
Data_Get_Struct(document, xmlDoc, doc);
doc = doc->doc; /* In case someone passes us a node. ugh. */
if (scanned_args == 1) {
parse_options = rb_const_get_at(rb_const_get_at(mNokogiriXml, rb_intern("ParseOptions")), rb_intern("DEFAULT_SCHEMA"));
}
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);
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);
rb_iv_set(rb_schema, "@parse_options", parse_options);
return rb_schema;
} Создать новую схему RelaxNG из документа Nokogiri::XML::Document doc
static VALUE
read_memory(int argc, VALUE *argv, VALUE klass)
{
VALUE content;
VALUE parse_options;
xmlRelaxNGParserCtxtPtr ctx;
xmlRelaxNGPtr schema;
VALUE errors;
VALUE rb_schema;
int scanned_args = 0;
scanned_args = rb_scan_args(argc, argv, "11", &content, &parse_options);
if (scanned_args == 1) {
parse_options = rb_const_get_at(rb_const_get_at(mNokogiriXml, rb_intern("ParseOptions")), rb_intern("DEFAULT_SCHEMA"));
}
ctx = xmlRelaxNGNewMemParserCtxt((const char *)StringValuePtr(content), (int)RSTRING_LEN(content));
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);
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);
rb_iv_set(rb_schema, "@parse_options", parse_options);
return rb_schema;
} Создать новую схему RelaxNG из содержимого string
© 2008–2023 by Mike Dalessio, Aaron Patterson, Yoko Harada, Akinori MUSHA, John Shahid,
Karol Bucek, Sam Ruby, Craig Barnes, Stephen Checkoway, Lars Kanis, Sergio Arbeo,
Timothy Elliott, Nobuyoshi Nakada, Charles Nutter, Patrick MahoneyLicensed under the MIT License.
https://nokogiri.org/rdoc/Nokogiri/XML/RelaxNG.html