### Home Classes Methods

### In Files

- lib/nokogiri/xml/schema.rb
- ext/nokogiri/html_document.c

### Parent

Object

### Methods

- ::from_document
- ::new
- ::read_memory
- #valid?
- #validate

### Files

- CHANGELOG.ja.rdoc
- CHANGELOG.rdoc
- C_CODING_STYLE.rdoc
- Manifest.txt
- README.ja.rdoc
- README.rdoc

### Class/Module Index

Quicksearch

- Nokogiri
- Nokogiri::CSS
- Nokogiri::CSS::Node
- Nokogiri::CSS::Parser
- Nokogiri::CSS::SyntaxError
- Nokogiri::CSS::Tokenizer
- Nokogiri::CSS::Tokenizer::ScanError
- Nokogiri::Decorators
- Nokogiri::Decorators::Slop
- Nokogiri::EncodingHandler
- Nokogiri::HTML
- Nokogiri::HTML::Builder
- Nokogiri::HTML::Document
- Nokogiri::HTML::Document::EncodingReader
- Nokogiri::HTML::Document::EncodingReader::JumpSAXHandler
- Nokogiri::HTML::DocumentFragment
- Nokogiri::HTML::ElementDescription
- Nokogiri::HTML::EntityDescription
- Nokogiri::HTML::EntityLookup
- Nokogiri::HTML::SAX
- Nokogiri::HTML::SAX::Parser
- Nokogiri::HTML::SAX::ParserContext
- Nokogiri::HTML::SAX::PushParser
- Nokogiri::SyntaxError
- Nokogiri::XML
- Nokogiri::XML::Attr
- Nokogiri::XML::AttributeDecl
- Nokogiri::XML::Builder
- Nokogiri::XML::CDATA
- Nokogiri::XML::CharacterData
- Nokogiri::XML::Comment
- Nokogiri::XML::DTD
- Nokogiri::XML::Document
- Nokogiri::XML::DocumentFragment
- Nokogiri::XML::Element
- Nokogiri::XML::ElementContent
- Nokogiri::XML::ElementDecl
- Nokogiri::XML::EntityDecl
- Nokogiri::XML::EntityReference
- Nokogiri::XML::Namespace
- Nokogiri::XML::Node
- Nokogiri::XML::Node::SaveOptions
- Nokogiri::XML::NodeSet
- Nokogiri::XML::Notation
- Nokogiri::XML::PP
- Nokogiri::XML::PP::CharacterData
- Nokogiri::XML::PP::Node
- Nokogiri::XML::ParseOptions
- Nokogiri::XML::ProcessingInstruction
- Nokogiri::XML::Reader
- Nokogiri::XML::RelaxNG
- Nokogiri::XML::SAX
- Nokogiri::XML::SAX::Document
- Nokogiri::XML::SAX::Parser
- Nokogiri::XML::SAX::Parser::Attribute
- Nokogiri::XML::SAX::ParserContext
- Nokogiri::XML::SAX::PushParser
- Nokogiri::XML::Schema
- Nokogiri::XML::SyntaxError
- Nokogiri::XML::Text
- Nokogiri::XML::XPath
- Nokogiri::XML::XPath::SyntaxError
- Nokogiri::XML::XPathContext
- Nokogiri::XSLT
- Nokogiri::XSLT::Stylesheet
- XSD
- XSD::XMLParser
- XSD::XMLParser::Nokogiri
- Object

No matching classes.

# Nokogiri::XML::Schema

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.message
    end

The list of errors are Nokogiri::XML::SyntaxError objects.

### Attributes

errors[RW]

Errors while parsing the schema file

### Public Class Methods

from_document(doc) click to toggle source

Create a new Schema from the Nokogiri::XML::Document `doc`

    static VALUE from_document(VALUE klass, VALUE document)
    {
      xmlDocPtr doc;
      xmlSchemaParserCtxtPtr ctx;
      xmlSchemaPtr schema;
      VALUE errors;
      VALUE rb_schema;

      Data_Get_Struct(document, xmlDoc, doc);

      /* In case someone passes us a node. ugh. */
      doc = doc->doc;

      ctx = xmlSchemaNewDocParserCtxt(doc);

      errors = rb_ary_new();
      xmlSetStructuredErrorFunc((void *)errors, Nokogiri_error_array_pusher);

    #ifdef HAVE_XMLSCHEMASETPARSERSTRUCTUREDERRORS
      xmlSchemaSetParserStructuredErrors(
        ctx,
        Nokogiri_error_array_pusher,
        (void *)errors
      );
    #endif

      schema = xmlSchemaParse(ctx);

      xmlSetStructuredErrorFunc(NULL, NULL);
      xmlSchemaFreeParserCtxt(ctx);

      if(NULL == schema) {
        xmlErrorPtr error = xmlGetLastError();
        if(error)
          Nokogiri_error_raise(NULL, error);
        else
          rb_raise(rb_eRuntimeError, "Could not parse document");

        return Qnil;
      }

      rb_schema = Data_Wrap_Struct(klass, 0, dealloc, schema);
      rb_iv_set(rb_schema, "@errors", errors);

      return rb_schema;

      return Qnil;
    }

new(string_or_io) click to toggle source

Create a new Nokogiri::XML::Schema object using a `string_or_io` object.

    # 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) click to toggle source

Create a new Schema from the contents of `string`

    static VALUE read_memory(VALUE klass, VALUE content)
    {
      xmlSchemaPtr schema;
      xmlSchemaParserCtxtPtr ctx = xmlSchemaNewMemParserCtxt(
          (const char *)StringValuePtr(content),
          (int)RSTRING_LEN(content)
      );
      VALUE rb_schema;
      VALUE errors = rb_ary_new();
      xmlSetStructuredErrorFunc((void *)errors, Nokogiri_error_array_pusher);

    #ifdef HAVE_XMLSCHEMASETPARSERSTRUCTUREDERRORS
      xmlSchemaSetParserStructuredErrors(
        ctx,
        Nokogiri_error_array_pusher,
        (void *)errors
      );
    #endif

       schema = xmlSchemaParse(ctx);

      xmlSetStructuredErrorFunc(NULL, NULL);
      xmlSchemaFreeParserCtxt(ctx);

      if(NULL == schema) {
        xmlErrorPtr error = xmlGetLastError();
        if(error)
          Nokogiri_error_raise(NULL, error);
        else
          rb_raise(rb_eRuntimeError, "Could not parse document");

        return Qnil;
      }

      rb_schema = Data_Wrap_Struct(klass, 0, dealloc, schema);
      rb_iv_set(rb_schema, "@errors", errors);

      return rb_schema;
    }

### Public Instance Methods

valid?(thing) click to toggle source

Returns true if `thing` is a valid Nokogiri::XML::Document or file.

    # File lib/nokogiri/xml/schema.rb, line 58
    def valid? thing
      validate(thing).length == 0
    end

validate(thing) click to toggle source

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.

    # File lib/nokogiri/xml/schema.rb, line 45
    def validate thing
      if thing.is_a?(Nokogiri::XML::Document)
        validate_document(thing)
      elsif File.file?(thing)
        validate_file(thing)
      else
        raise ArgumentError, "Must provide Nokogiri::Xml::Document or the name of an existing file"
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.