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, Namespaces, Type

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cached(location) ⇒ Object



61
62
63
64
# File 'lib/libxml/schema.rb', line 61

def self.cached(location)
  @_schemas ||= {}
  @_schemas[location] ||= new(location)
end

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

Create a new schema from the specified document.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'ext/libxml/ruby_xml_schema.c', line 80

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") ⇒ Object

Create a new schema using the specified string.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'ext/libxml/ruby_xml_schema.c', line 104

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) ⇒ Object

Create a new schema from the specified URI.



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'ext/libxml/ruby_xml_schema.c', line 60

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

Instance Method Details

#_collect_typesObject



266
267
268
269
270
271
272
273
274
275
276
277
# File 'ext/libxml/ruby_xml_schema.c', line 266

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

#_namespacesObject



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

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

#documentObject



157
158
159
160
161
162
163
164
# File 'ext/libxml/ruby_xml_schema.c', line 157

static VALUE rxml_schema_document(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

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

#elementsObject



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'ext/libxml/ruby_xml_schema.c', line 243

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

#idObject



147
148
149
150
151
152
153
154
# File 'ext/libxml/ruby_xml_schema.c', line 147

static VALUE rxml_schema_id(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  QNIL_OR_STRING(xschema->id)
}

#nameObject



129
130
131
132
133
134
135
136
# File 'ext/libxml/ruby_xml_schema.c', line 129

static VALUE rxml_schema_name(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  QNIL_OR_STRING(xschema->name)
}

#namespacesObject



57
58
59
# File 'lib/libxml/schema.rb', line 57

def namespaces
  Namespaces.new(_namespaces.uniq { |n| n.href })
end

#target_namespaceObject



120
121
122
123
124
125
126
127
# File 'ext/libxml/ruby_xml_schema.c', line 120

static VALUE rxml_schema_target_namespace(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  QNIL_OR_STRING(xschema->targetNamespace)
}

#typesObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'ext/libxml/ruby_xml_schema.c', line 215

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

#versionObject



138
139
140
141
142
143
144
145
# File 'ext/libxml/ruby_xml_schema.c', line 138

static VALUE rxml_schema_version(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  QNIL_OR_STRING(xschema->version)
}