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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'ext/nokogiri/xml_schema.c', line 100 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
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'ext/nokogiri/xml_schema.c', line 54 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.
56 57 58 |
# File 'lib/nokogiri/xml/schema.rb', line 56 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 48 49 50 51 |
# File 'lib/nokogiri/xml/schema.rb', line 45 def validate thing return validate_document(thing) if thing.is_a?(Nokogiri::XML::Document) # FIXME libxml2 has an api for validating files. We should switch # to that because it will probably save memory. validate_document(Nokogiri::XML(File.read(thing))) end |