Sha256: 1c1b34b7aeb7d9ad354e00365e569c47db6b480afe75ae7fbecf7570f730e1f5

Contents?: true

Size: 1.82 KB

Versions: 8

Compression:

Stored size: 1.82 KB

Contents

module Nokogiri
  module XML
    class << self
      ###
      # Create a new Nokogiri::XML::Schema object using a +string_or_io+
      # object.
      def Schema string_or_io
        Schema.new(string_or_io)
      end
    end

    ###
    # 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.
    class Schema
      # Errors while parsing the schema file
      attr_accessor :errors

      ###
      # Create a new Nokogiri::XML::Schema object using a +string_or_io+
      # object.
      def self.new string_or_io
        if string_or_io.respond_to?(:read)
          string_or_io = string_or_io.read
        end

        read_memory(string_or_io)
      end

      ###
      # 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.
      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

      ###
      # Returns true if +thing+ is a valid Nokogiri::XML::Document or
      # file.
      def valid? thing
        validate(thing).length == 0
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
nokogiri-1.3.0-x86-mingw32 lib/nokogiri/xml/schema.rb
nokogiri-1.3.0-java lib/nokogiri/xml/schema.rb
nokogiri-1.3.1-x86-mingw32 lib/nokogiri/xml/schema.rb
nokogiri-1.3.0 lib/nokogiri/xml/schema.rb
nokogiri-1.3.0-x86-mswin32 lib/nokogiri/xml/schema.rb
nokogiri-1.3.1-java lib/nokogiri/xml/schema.rb
nokogiri-1.3.1-x86-mswin32 lib/nokogiri/xml/schema.rb
nokogiri-1.3.1 lib/nokogiri/xml/schema.rb