Class: LibXML::XML::Schema

Inherits:
Object
  • Object
show all
Defined in:
ext/libxml/ruby_xml_schema.c,
lib/libxml/schema.rb,
ext/libxml/ruby_xml_schema.c

Overview

The XML::Schema class is used to prepare XML Schemas for validation of xml documents.

Schemas can be created from XML documents, strinings or URIs using the corresponding methods (new for URIs).

Once a schema is prepared, an XML document can be validated by the XML::Document#validate_schema method providing the XML::Schema object as parameter. The method return true if the document validates, false otherwise.

Basic usage:

# parse schema as xml document
schema_document = XML::Document.file('schema.rng')

# prepare schema for validation
schema = XML::Schema.document(schema_document)

# parse xml document to be validated
instance = XML::Document.file('instance.xml')

# validate
instance.validate_schema(schema)

Defined Under Namespace

Modules: Types Classes: Attribute, Element, Facet, Type

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject (readonly)

#nameObject (readonly)

#target_namespaceObject (readonly)

Create attr_reader methods for the above instance variables

#versionObject (readonly)

Class Method Details

.XML::Schema.document(document) ⇒ Object

Create a new schema from the specified document.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'ext/libxml/ruby_xml_schema.c', line 184

static VALUE rxml_schema_init_from_document(VALUE class, VALUE document)
{
  xmlDocPtr xdoc;
  xmlSchemaParserCtxtPtr xparser;

  TypedData_Get_Struct(document, xmlDoc, &rxml_document_data_type, xdoc);

  xmlResetLastError();
  xparser = xmlSchemaNewDocParserCtxt(xdoc);
  if (!xparser)
    rxml_raise(xmlGetLastError());

  return rxml_schema_init(class, xparser);
}

.XML::Schema.from_string("schema_data") ⇒ Object

Create a new schema using the specified string.



205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'ext/libxml/ruby_xml_schema.c', line 205

static VALUE rxml_schema_init_from_string(VALUE class, VALUE schema_str)
{
  xmlSchemaParserCtxtPtr xparser;

  Check_Type(schema_str, T_STRING);

  xmlResetLastError();
  xparser = xmlSchemaNewMemParserCtxt(StringValuePtr(schema_str), (int)strlen(StringValuePtr(schema_str)));
  if (!xparser)
    rxml_raise(xmlGetLastError());

  return rxml_schema_init(class, xparser);
}

.XML::Schema.initialize(schema_uri) ⇒ Object

Create a new schema from the specified URI.



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'ext/libxml/ruby_xml_schema.c', line 164

static VALUE rxml_schema_init_from_uri(VALUE class, VALUE uri)
{
  xmlSchemaParserCtxtPtr xparser;

  Check_Type(uri, T_STRING);

  xmlResetLastError();
  xparser = xmlSchemaNewParserCtxt(StringValuePtr(uri));
  if (!xparser)
    rxml_raise(xmlGetLastError());

  return rxml_schema_init(class, xparser);
}

Instance Method Details

#XML::Schema.documentObject

Return the Schema XML Document



225
226
227
228
229
230
231
232
# File 'ext/libxml/ruby_xml_schema.c', line 225

static VALUE rxml_schema_document(VALUE self)
{
  xmlSchemaPtr xschema;

  TypedData_Get_Struct(self, xmlSchema, &rxml_schema_type, xschema);

  return rxml_node_wrap(xmlDocGetRootElement(xschema->doc));
}

#elementsObject



278
279
280
281
282
283
284
285
286
287
# File 'ext/libxml/ruby_xml_schema.c', line 278

static VALUE rxml_schema_elements(VALUE self)
{
  VALUE result = rb_hash_new();
  xmlSchemaPtr xschema;

  TypedData_Get_Struct(self, xmlSchema, &rxml_schema_type, xschema);
  xmlHashScan(xschema->elemDecl, (xmlHashScanner)scan_schema_element, (void *)result);

  return result;
}

#XML::Schema.imported_ns_elementsHash

Returns a hash by namespace of a hash of schema elements within the entire schema including imports

Returns:

  • (Hash)


305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'ext/libxml/ruby_xml_schema.c', line 305

static VALUE rxml_schema_imported_ns_elements(VALUE self)
{
  xmlSchemaPtr xschema;
  VALUE result = rb_hash_new();

  TypedData_Get_Struct(self, xmlSchema, &rxml_schema_type, xschema);

  if (xschema)
  {
    xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_ns_elements, (void *)result);
  }

  return result;
}

#XML::Schema.imported_ns_typesHash

Returns a hash by namespace of a hash of schema types within the entire schema including imports

Returns:

  • (Hash)


386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'ext/libxml/ruby_xml_schema.c', line 386

static VALUE rxml_schema_imported_ns_types(VALUE self)
{
  xmlSchemaPtr xschema;
  VALUE result = rb_hash_new();

  TypedData_Get_Struct(self, xmlSchema, &rxml_schema_type, xschema);

  if (xschema)
  {
    xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_ns_types, (void *)result);
  }

  return result;
}

#XML::Schema.imported_typesHash

Returns a hash of all types within the entire schema including imports

Returns:

  • (Hash)


355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'ext/libxml/ruby_xml_schema.c', line 355

static VALUE rxml_schema_imported_types(VALUE self)
{
  xmlSchemaPtr xschema;
  VALUE result = rb_hash_new();

  TypedData_Get_Struct(self, xmlSchema, &rxml_schema_type, xschema);

  if (xschema)
  {
    xmlHashScan(xschema->schemasImports, (xmlHashScanner)collect_imported_types, (void *)result);
  }

  return result;
}

#XML::Schema.namespacesArray

Returns an array of Namespaces defined by the schema

Returns:

  • (Array)


259
260
261
262
263
264
265
266
267
268
269
270
# File 'ext/libxml/ruby_xml_schema.c', line 259

static VALUE rxml_schema_namespaces(VALUE self)
{
  VALUE result;
  xmlSchemaPtr xschema;

  TypedData_Get_Struct(self, xmlSchema, &rxml_schema_type, xschema);

  result = rb_ary_new();
  xmlHashScan(xschema->schemasImports, (xmlHashScanner)scan_namespaces, (void *)result);

  return result;
}

#typesObject



326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'ext/libxml/ruby_xml_schema.c', line 326

static VALUE rxml_schema_types(VALUE self)
{
  VALUE result = rb_hash_new();
  xmlSchemaPtr xschema;

  TypedData_Get_Struct(self, xmlSchema, &rxml_schema_type, xschema);

  if (xschema != NULL && xschema->typeDecl != NULL)
  {
    xmlHashScan(xschema->typeDecl, (xmlHashScanner)scan_schema_type, (void *)result);
  }

  return result;
}