class LibXML::XML::Schema

  1. ext/libxml/libxml.c
  2. lib/libxml/schema.rb
  3. show all
Parent: XML

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 LibXML::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)

Public Class methods

cached (location)
[show source]
# File lib/libxml/schema.rb, line 61
def self.cached(location)
  @_schemas ||= {}
  @_schemas[location] ||= new(location)
end
XML::Schema.document(document) -> schema

Create a new schema from the specified document.

[show source]
static VALUE rxml_schema_init_from_document(VALUE class, VALUE document)
{
  xmlDocPtr xdoc;
  xmlSchemaPtr xschema;
  xmlSchemaParserCtxtPtr xparser;

  Data_Get_Struct(document, xmlDoc, xdoc);

  xparser = xmlSchemaNewDocParserCtxt(xdoc);
  xschema = xmlSchemaParse(xparser);
  xmlSchemaFreeParserCtxt(xparser);

  return Data_Wrap_Struct(cXMLSchema, NULL, rxml_schema_free, xschema);
}
XML::Schema.string("schema_data") -> "value"

Create a new schema using the specified string.

[show source]
static VALUE rxml_schema_init_from_string(VALUE self, VALUE schema_str)
{
  xmlSchemaParserCtxtPtr xparser;
  xmlSchemaPtr xschema;

  Check_Type(schema_str, T_STRING);

  xparser = xmlSchemaNewMemParserCtxt(StringValuePtr(schema_str), strlen(
      StringValuePtr(schema_str)));
  xschema = xmlSchemaParse(xparser);
  xmlSchemaFreeParserCtxt(xparser);

  return Data_Wrap_Struct(cXMLSchema, NULL, rxml_schema_free, xschema);
}
XML::Schema.initialize(schema_uri) -> schema

Create a new schema from the specified URI.

[show source]
static VALUE rxml_schema_init_from_uri(VALUE class, VALUE uri)
{
  xmlSchemaParserCtxtPtr xparser;
  xmlSchemaPtr xschema;

  Check_Type(uri, T_STRING);

  xparser = xmlSchemaNewParserCtxt(StringValuePtr(uri));
  xschema = xmlSchemaParse(xparser);
  xmlSchemaFreeParserCtxt(xparser);

  return Data_Wrap_Struct(cXMLSchema, NULL, rxml_schema_free, xschema);
}

Public Instance methods

_collect_types ()
[show source]
static VALUE rxml_schema_collect_types(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  if(xschema){
    xmlHashScan(xschema->schemasImports, (xmlHashScanner) collectSchemaTypes, (void *)self);
  }

  return Qnil;
}
_namespaces ()
[show source]
static VALUE rxml_schema_namespaces(VALUE self)
{
  VALUE schemas;
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  if (rb_iv_get(self, "@namespaces") == Qnil) {
    schemas = rb_ary_new();
    rb_iv_set(self, "@namespaces", schemas);
    xmlHashScan(xschema->schemasImports, (xmlHashScanner) storeNs, (void *)self);
  }

  return rb_iv_get(self, "@namespaces");
}
document ()
[show source]
static VALUE rxml_schema_document(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  return rxml_node_wrap(xmlDocGetRootElement(xschema->doc));
}
elements ()
[show source]
static VALUE rxml_schema_elements(VALUE self)
{
  VALUE elements;
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  if (rb_iv_get(self, "@elements") == Qnil) {
    elements = rb_hash_new();
    rb_iv_set(self, "@elements", elements);
    xmlHashScan(xschema->elemDecl, (xmlHashScanner) storeElement, (void *)self);
  }

  return rb_iv_get(self, "@elements");
}
id ()
[show source]
static VALUE rxml_schema_id(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  QNIL_OR_STRING(xschema->id)
}
name ()
[show source]
static VALUE rxml_schema_name(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  QNIL_OR_STRING(xschema->name)
}
namespaces ()
[show source]
# File lib/libxml/schema.rb, line 57
def namespaces
  Namespaces.new(_namespaces.uniq { |n| n.href })
end
target_namespace ()
[show source]
static VALUE rxml_schema_target_namespace(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  QNIL_OR_STRING(xschema->targetNamespace)
}
types ()
[show source]
static VALUE rxml_schema_types(VALUE self)
{
  VALUE types;
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  if (rb_iv_get(self, "@types") == Qnil) {
    types = rb_hash_new();
    rb_iv_set(self, "@types", types);
    rxml_schema_collect_types(self);
    if(xschema != NULL && xschema->typeDecl != NULL)
      xmlHashScan(xschema->typeDecl, (xmlHashScanner) storeType, (void *)self);
  }

  return rb_iv_get(self, "@types");
}
version ()
[show source]
static VALUE rxml_schema_version(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  QNIL_OR_STRING(xschema->version)
}