class SchematronNokogiri::Schema

Constants

ISO_FILES

The file names of the compilation stages

ISO_IMPL_DIR

The location of the ISO schematron implemtation lives

NS_PREFIXES

Namespace prefix declarations for use in XPaths

Public Class Methods

new(doc) click to toggle source
# File lib/schematron-nokogiri.rb, line 20
def initialize(doc)
  schema_doc = doc

  xforms = ISO_FILES.map do |file|

    Dir.chdir(ISO_IMPL_DIR) do
      Nokogiri::XSLT(File.open(file))
    end

  end

  # Compile schematron into xsl that maps to svrl
  @validator_doc = xforms.inject(schema_doc) {
      |xml, xsl| xsl.transform xml
  }
  @validator_xsl = Nokogiri::XSLT(@validator_doc.to_s)
end

Public Instance Methods

node_type(node) click to toggle source
# File lib/schematron-nokogiri.rb, line 89
def node_type(node)
  case
    when node.cdata?
      'cdata'
    when node.comment?
      'comment'
    when node.element?
      'element'
    when node.fragment?
      'fragment'
  end
end
prefixes() click to toggle source
# File lib/schematron-nokogiri.rb, line 48
def prefixes
  namespaces = {}

  @validator_doc.namespaces.each {
      |k, v| namespaces[k.gsub 'xmlns:', ''] = v
  }
  namespaces
end
rule_hits(results_doc, instance_doc, rule_type, xpath) click to toggle source

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

# File lib/schematron-nokogiri.rb, line 58
def rule_hits(results_doc, instance_doc, rule_type, xpath)

  results = []

  results_doc.root.xpath(xpath, NS_PREFIXES).each do |hit|
    context_tag = hit
    context_path = nil
    while context_path.nil?
      context_tag = context_tag.previous_sibling
      context_path = context_tag['context']
    end

    context = instance_doc.root.xpath(
        context_path ? '/' + context_path : hit['location'],
        NS_PREFIXES.merge(prefixes)
    ).first

    hit.xpath('svrl:text/text()', NS_PREFIXES).each do |message|
      results << {
          :rule_type => rule_type,
          :type => context ? node_type(context) : nil,
          :name => context ? context.name : nil,
          :line => context ? context.line : nil,
          :message => message.content.strip}
    end
  end

  results

end
validate(instance_doc) click to toggle source
# File lib/schematron-nokogiri.rb, line 38
def validate(instance_doc)

  # Validate the xml
  results_doc = @validator_xsl.transform 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