Class: Nokogiri::XML::Schema
- Inherits:
-
Object
- Object
- Nokogiri::XML::Schema
- Defined in:
- lib/nokogiri/xml/schema.rb,
lib/nokogiri/ffi/xml/schema.rb,
ext/nokogiri/xml_schema.c
Overview
Nokogiri::XML::Schema is used for validating XML against a schema (usually from an xsd file).
Synopsis
Validate an XML document against a Schema. Loop over the errors that are returned and print them out:
xsd = Nokogiri::XML::Schema(File.read(PO_SCHEMA_FILE))
doc = Nokogiri::XML(File.read(PO_XML_FILE))
xsd.validate(doc).each do |error|
puts error.
end
The list of errors are Nokogiri::XML::SyntaxError objects.
Instance Attribute Summary collapse
-
#cstruct ⇒ Object
:stopdoc:.
-
#errors ⇒ Object
Errors while parsing the schema file.
Class Method Summary collapse
-
.from_document(doc) ⇒ Object
Create a new Schema from the Nokogiri::XML::Document
doc
. -
.new(string_or_io) ⇒ Object
Create a new Nokogiri::XML::Schema object using a
string_or_io
object. -
.read_memory(string) ⇒ Object
Create a new Schema from the contents of
string
.
Instance Method Summary collapse
-
#valid?(thing) ⇒ Boolean
Returns true if
thing
is a valid Nokogiri::XML::Document or file. -
#validate(thing) ⇒ Object
Validate
thing
against this schema.
Instance Attribute Details
#cstruct ⇒ Object
:stopdoc:
6 7 8 |
# File 'lib/nokogiri/ffi/xml/schema.rb', line 6 def cstruct @cstruct end |
#errors ⇒ Object
Errors while parsing the schema file
31 32 33 |
# File 'lib/nokogiri/xml/schema.rb', line 31 def errors @errors end |
Class Method Details
.from_document(doc) ⇒ Object
Create a new Schema from the Nokogiri::XML::Document doc
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'ext/nokogiri/xml_schema.c', line 142 def self.from_document document ctx = LibXML.xmlSchemaNewDocParserCtxt(document.document.cstruct) errors = [] LibXML.xmlSetStructuredErrorFunc(nil, SyntaxError.error_array_pusher(errors)) unless Nokogiri.is_2_6_16? LibXML.xmlSchemaSetParserStructuredErrors( ctx, SyntaxError.error_array_pusher(errors), nil ) end schema_ptr = LibXML.xmlSchemaParse(ctx) LibXML.xmlSetStructuredErrorFunc(nil, nil) LibXML.xmlSchemaFreeParserCtxt(ctx) if schema_ptr.null? error = LibXML.xmlGetLastError if error raise SyntaxError.wrap(error) else raise RuntimeError, "Could not parse document" end end schema = allocate schema.cstruct = LibXML::XmlSchema.new schema_ptr schema.errors = errors schema end |
.new(string_or_io) ⇒ Object
Create a new Nokogiri::XML::Schema object using a string_or_io
object.
36 37 38 |
# File 'lib/nokogiri/xml/schema.rb', line 36 def self.new string_or_io from_document Nokogiri::XML(string_or_io) end |
.read_memory(string) ⇒ Object
Create a new Schema from the contents of string
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'ext/nokogiri/xml_schema.c', line 96 def self.read_memory content content_copy = FFI::MemoryPointer.from_string(content) ctx = LibXML.xmlSchemaNewMemParserCtxt(content_copy, content.length) errors = [] LibXML.xmlSetStructuredErrorFunc(nil, SyntaxError.error_array_pusher(errors)) LibXML.xmlSchemaSetParserStructuredErrors(ctx, SyntaxError.error_array_pusher(errors), nil) unless Nokogiri.is_2_6_16? schema_ptr = LibXML.xmlSchemaParse(ctx) LibXML.xmlSetStructuredErrorFunc(nil, nil) LibXML.xmlSchemaFreeParserCtxt(ctx) if schema_ptr.null? error = LibXML.xmlGetLastError if error raise SyntaxError.wrap(error) else raise RuntimeError, "Could not parse document" end end schema = allocate schema.cstruct = LibXML::XmlSchema.new schema_ptr schema.errors = errors schema end |
Instance Method Details
#valid?(thing) ⇒ Boolean
Returns true if thing
is a valid Nokogiri::XML::Document or file.
52 53 54 |
# File 'lib/nokogiri/xml/schema.rb', line 52 def valid? thing validate(thing).length == 0 end |
#validate(thing) ⇒ Object
Validate thing
against this schema. thing
can be a Nokogiri::XML::Document object, or a filename. An Array of Nokogiri::XML::SyntaxError objects found while validating the thing
is returned.
45 46 47 |
# File 'lib/nokogiri/xml/schema.rb', line 45 def validate thing thing.is_a?(Nokogiri::XML::Document) ? validate_document(thing) : validate_file(thing) end |