class LibXML::XML::Schema

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)

Attributes

id[R]
name[R]
target_namespace[R]

Create attr_reader methods for the above instance variables

version[R]

Public Class Methods

XML::Schema.document(document) → schema click to toggle source

Create a new schema from the specified document.

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") → "value" click to toggle source

Create a new schema using the specified string.

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) → schema click to toggle source

Create a new schema from the specified URI.

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);
}

Public Instance Methods

XML::Schema.document → document click to toggle source

Return the Schema XML Document

static VALUE rxml_schema_document(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  return rxml_node_wrap(xmlDocGetRootElement(xschema->doc));
}
elements() click to toggle source
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_elements → hash click to toggle source

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

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_types → hash click to toggle source

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

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_types → hash click to toggle source

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

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.namespaces → array click to toggle source

Returns an array of Namespaces defined by the schema

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;
}
types() click to toggle source
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;
}