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 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)
click to toggle source
# File lib/libxml/schema.rb, line 61 def self.cached(location) @_schemas ||= {} @_schemas[location] ||= new(location) end
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; xmlSchemaPtr xschema; xmlSchemaParserCtxtPtr xparser; Data_Get_Struct(document, xmlDoc, xdoc); xparser = xmlSchemaNewDocParserCtxt(xdoc); xschema = xmlSchemaParse(xparser); xmlSchemaFreeParserCtxt(xparser); if (xschema == NULL) return Qnil; return Data_Wrap_Struct(cXMLSchema, NULL, rxml_schema_free, xschema); }
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 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
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; 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()
click to toggle 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()
click to toggle 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()
click to toggle source
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 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()
click to toggle source
static VALUE rxml_schema_id(VALUE self) { xmlSchemaPtr xschema; Data_Get_Struct(self, xmlSchema, xschema); QNIL_OR_STRING(xschema->id) }
name()
click to toggle source
static VALUE rxml_schema_name(VALUE self) { xmlSchemaPtr xschema; Data_Get_Struct(self, xmlSchema, xschema); QNIL_OR_STRING(xschema->name) }
namespaces()
click to toggle source
# File lib/libxml/schema.rb, line 57 def namespaces Namespaces.new(_namespaces.uniq { |n| n.href }) end
target_namespace()
click to toggle source
static VALUE rxml_schema_target_namespace(VALUE self) { xmlSchemaPtr xschema; Data_Get_Struct(self, xmlSchema, xschema); QNIL_OR_STRING(xschema->targetNamespace) }
types()
click to toggle 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()
click to toggle source
static VALUE rxml_schema_version(VALUE self) { xmlSchemaPtr xschema; Data_Get_Struct(self, xmlSchema, xschema); QNIL_OR_STRING(xschema->version) }