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

#rule_hits(results_doc, instance_doc, rule_type, xpath) ⇒ Object

Look for reported or failed rules of a particular type in the instance doc



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/schematron.rb', line 52

def rule_hits(results_doc, instance_doc, rule_type, xpath)
  
  results = []
  
  results_doc.root.find(xpath, NS_PREFIXES).each do |hit|
    context = instance_doc.root.find_first hit['location']
    
    hit.find('svrl:text/text()', NS_PREFIXES).each do |message|
      results << {
        :rule_type => rule_type,
        :type => context.node_type_name,
        :name => context.name,
        :line => context.line_num,
        :message => message.content.strip }
    end
  end
  
  results
  
end

#validate(instance_doc) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/schematron.rb', line 41

def validate(instance_doc)

  # Validate the xml
  results_doc = @validator_xsl.apply instance_doc

  # compile the errors and log any messages
  rule_hits(results_doc, instance_doc, 'assert', '//svrl:failed-assert') +
  rule_hits(results_doc, instance_doc, 'report', '//svrl:successful-report')
end