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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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

Create a new schema from the specified document.



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

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.



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

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), (int)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.



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

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

#documentObject



154
155
156
157
158
159
160
161
# File 'ext/libxml/ruby_xml_schema.c', line 154

static VALUE rxml_schema_document(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

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

#elementsObject



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

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_element, (void *)result);

  return result;
}

#idObject



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

static VALUE rxml_schema_id(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  QNIL_OR_STRING(xschema->id)
}

#imported_typesObject



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

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

#nameObject



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

static VALUE rxml_schema_name(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  QNIL_OR_STRING(xschema->name)
}

#namespacesObject



182
183
184
185
186
187
188
189
190
191
192
193
# File 'ext/libxml/ruby_xml_schema.c', line 182

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

#target_namespaceObject



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

static VALUE rxml_schema_target_namespace(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  QNIL_OR_STRING(xschema->targetNamespace)
}

#typesObject



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

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_type, (void *)result);
  }

  return result;
}

#versionObject



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

static VALUE rxml_schema_version(VALUE self)
{
  xmlSchemaPtr xschema;

  Data_Get_Struct(self, xmlSchema, xschema);

  QNIL_OR_STRING(xschema->version)
}