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.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'ext/libxml/ruby_xml_schema.c', line 177

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

  Data_Get_Struct(document, xmlDoc, 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.



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'ext/libxml/ruby_xml_schema.c', line 198

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.



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'ext/libxml/ruby_xml_schema.c', line 157

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



218
219
220
221
222
223
224
225
# File 'ext/libxml/ruby_xml_schema.c', line 218

static VALUE rxml_schema_document(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

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

#elementsObject



271
272
273
274
275
276
277
278
279
280
# File 'ext/libxml/ruby_xml_schema.c', line 271

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

  Data_Get_Struct(self, xmlSchema, 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)


298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'ext/libxml/ruby_xml_schema.c', line 298

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

  Data_Get_Struct(self, xmlSchema, 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)


379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'ext/libxml/ruby_xml_schema.c', line 379

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

  Data_Get_Struct(self, xmlSchema, 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)


348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'ext/libxml/ruby_xml_schema.c', line 348

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

  Data_Get_Struct(self, xmlSchema, 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)


252
253
254
255
256
257
258
259
260
261
262
263
# File 'ext/libxml/ruby_xml_schema.c', line 252

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

  Data_Get_Struct(self, xmlSchema, xschema);

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

  return result;
}

#typesObject



319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'ext/libxml/ruby_xml_schema.c', line 319

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

  Data_Get_Struct(self, xmlSchema, xschema);

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

  return result;
}