Sha256: 264b0f442b0194d95a2c1592c50176a283250bc66e7e7bbd518edc4c77cb317a

Contents?: true

Size: 1.74 KB

Versions: 27

Compression:

Stored size: 1.74 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
        from_document Nokogiri::XML(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

27 entries across 27 versions & 7 rubygems

Version Path
rubyjedi-nokogiri_java-1.4.0.20100513161003-java lib/nokogiri/xml/schema.rb
jwagener-nokogiri-1.4.1 lib/nokogiri/xml/schema.rb
akamai_bookmarklet-0.1.2 vendor/gems/ruby/1.8/gems/nokogiri-1.3.3/lib/nokogiri/xml/schema.rb
revo-nokogiri-1.4.1-java lib/nokogiri/xml/schema.rb
revo-nokogiri-1.4.0.3-java lib/nokogiri/xml/schema.rb
revo-nokogiri-1.4.0-java lib/nokogiri/xml/schema.rb
revo-nokogiri-1.4.0.20100202165440-java lib/nokogiri/xml/schema.rb
akamai_bookmarklet-0.1.1 vendor/gems/ruby/1.8/gems/nokogiri-1.3.3/lib/nokogiri/xml/schema.rb
akamai_bookmarklet-0.1.0 vendor/gems/ruby/1.8/gems/nokogiri-1.3.3/lib/nokogiri/xml/schema.rb
nokogiri-1.4.1-x86-mswin32 lib/nokogiri/xml/schema.rb
nokogiri-1.4.1-x86-mingw32 lib/nokogiri/xml/schema.rb
nokogiri-1.4.1-java lib/nokogiri/xml/schema.rb
nokogiri-1.4.1 lib/nokogiri/xml/schema.rb
superfeedr-nokogiri-1.4.0.20091116183308 lib/nokogiri/xml/schema.rb
caring-nokogiri-1.4.1.pre1 lib/nokogiri/xml/schema.rb
nokogiri-1.4.0-java lib/nokogiri/xml/schema.rb
nokogiri-1.4.0-x86-mswin32 lib/nokogiri/xml/schema.rb
nokogiri-1.4.0-x86-mingw32 lib/nokogiri/xml/schema.rb
nokogiri-1.4.0 lib/nokogiri/xml/schema.rb
nokogiri-1.3.3-x86-mswin32 lib/nokogiri/xml/schema.rb