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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject

Errors while parsing the schema file



31
32
33
# File 'lib/nokogiri/xml/schema.rb', line 31

def errors
  @errors
end

Class Method Details

.from_document(doc) ⇒ Object

Create a new Schema from the Nokogiri::XML::Document doc



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'ext/nokogiri/xml_schema.c', line 142

static VALUE from_document(VALUE klass, VALUE document)
{
  xmlDocPtr doc;
  xmlSchemaParserCtxtPtr ctx;
  xmlSchemaPtr schema;
  VALUE errors;
  VALUE rb_schema;

  Data_Get_Struct(document, xmlDoc, doc);

  /* In case someone passes us a node. ugh. */
  doc = doc->doc;

  ctx = xmlSchemaNewDocParserCtxt(doc);

  errors = rb_ary_new();
  xmlSetStructuredErrorFunc((void *)errors, Nokogiri_error_array_pusher);

#ifdef HAVE_XMLSCHEMASETPARSERSTRUCTUREDERRORS
  xmlSchemaSetParserStructuredErrors(
    ctx,
    Nokogiri_error_array_pusher,
    (void *)errors
  );
#endif

  schema = xmlSchemaParse(ctx);

  xmlSetStructuredErrorFunc(NULL, NULL);
  xmlSchemaFreeParserCtxt(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;

  return Qnil;
}

.new(string_or_io) ⇒ Object

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



36
37
38
# File 'lib/nokogiri/xml/schema.rb', line 36

def self.new string_or_io
  from_document Nokogiri::XML(string_or_io)
end

.read_memory(string) ⇒ Object

Create a new Schema from the contents of string



96
97
98
99
100
101
102
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
# File 'ext/nokogiri/xml_schema.c', line 96

static VALUE read_memory(VALUE klass, VALUE content)
{
  xmlSchemaPtr schema;
  xmlSchemaParserCtxtPtr ctx = xmlSchemaNewMemParserCtxt(
      (const char *)StringValuePtr(content),
      (int)RSTRING_LEN(content)
  );
  VALUE rb_schema;
  VALUE errors = rb_ary_new();
  xmlSetStructuredErrorFunc((void *)errors, Nokogiri_error_array_pusher);

#ifdef HAVE_XMLSCHEMASETPARSERSTRUCTUREDERRORS
  xmlSchemaSetParserStructuredErrors(
    ctx,
    Nokogiri_error_array_pusher,
    (void *)errors
  );
#endif

   schema = xmlSchemaParse(ctx);

  xmlSetStructuredErrorFunc(NULL, NULL);
  xmlSchemaFreeParserCtxt(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;
}

Instance Method Details

#valid?(thing) ⇒ Boolean

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



58
59
60
# File 'lib/nokogiri/xml/schema.rb', line 58

def valid? thing
  validate(thing).length == 0
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.



45
46
47
48
49
50
51
52
53
# File 'lib/nokogiri/xml/schema.rb', line 45

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