Sha256: e2de89973b9ad692e4a8a56dce96b6ac7218f1c1458cd0e931edec8755f6778e

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

require 'libxml'
require 'libxslt'

module Schematron

  include LibXML
  include LibXSLT

  # The location of the ISO schematron implemtation lives
  ISO_IMPL_DIR = 'iso_impl'

  # The file names of the compilation stages
  ISO_FILES = [ 'iso_dsdl_include.xsl',
                'iso_abstract_expand.xsl',
                'iso_svrl.xsl' ]

  # Namespace prefix declarations for use in XPaths
  NS_PREFIXES = {
    'svrl' => 'http://purl.oclc.org/dsdl/svrl'
  }

  class Schema

    def initialize(doc)
      @schema_doc = doc
    end

    def validate(instance_doc)

      # Compile schematron into xsl that maps to svrl
      xforms = ISO_FILES.map do |file|

        Dir.chdir(ISO_IMPL_DIR) do
          doc = XML::Document.file file
          LibXSLT::XSLT::Stylesheet.new doc
        end

      end

      validator_doc = xforms.inject(@schema_doc) { |xml, xsl| xsl.apply xml }
      validator_xsl = LibXSLT::XSLT::Stylesheet.new validator_doc

      # Validate the xml
      results_doc = validator_xsl.apply instance_doc

      # compile the errors
      results = []
      results_doc.root.find('//svrl:failed-assert', NS_PREFIXES).each do |assert|
        context = instance_doc.root.find_first assert['location']

        assert.find('svrl:text/text()', NS_PREFIXES).each do |message|
          results << {
            :type => context.node_type_name,
            :name => context.name,
            :line => context.line_num,
            :message => message.content.strip
          }
        end

      end
      
      results
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
schematron-0.0.0 lib/schematron.rb