Class: Schematron::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/schematron.rb

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ Schema

Returns a new instance of Schema.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/schematron.rb', line 24

def initialize(doc)
  schema_doc = doc

  xforms = ISO_FILES.map do |file|

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

  end
  
  # Compile schematron into xsl that maps to svrl
  validator_doc = xforms.inject(schema_doc) { |xml, xsl| xsl.apply xml }
  @validator_xsl = LibXSLT::XSLT::Stylesheet.new validator_doc
end

Instance Method Details

#validate(instance_doc) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/schematron.rb', line 41

def validate(instance_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