Class: Schematronium

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

Overview

Library for running Schematron validators over XML documents

Instance Method Summary collapse

Constructor Details

#initialize(schematron, phase = "'#ALL'") ⇒ Schematronium

Constructor for schematron checker

Parameters:

  • schematron (String, IO, File)

    A schematron document, as either an IO object responding to #read, a filename, or a [String]

  • phase (String) (defaults to: "'#ALL'")

    The name of the schematron phase to run. By default, the special “run everything” phase is run


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/schematronium.rb', line 12

def initialize(schematron, phase="'#ALL'")
  stages = %w|iso_dsdl_include.xsl
              iso_abstract_expand.xsl
              iso_svrl_for_xslt2.xsl|.map{|s| iso_file s}

  schematron = if schematron.respond_to? :read
                 Saxon.XML(schematron.read)
               elsif schematron.kind_of? String
                 if File.file? schematron
                   Saxon.XML(File.open(schematron))
                 else
                   Saxon.XML(schematron)
                 end
               else
                 raise "Unable to generate Schematron document from #{schematron.class.to_s}"
               end

  # Run schematron through each stage of the iso_schematron pipeline
  #    then stringify the final result because Saxon.XSLT can't take
  #    an XML doc as input
  @sch_script = stages[0].transform(schematron)
  @sch_script = stages[1].transform(@sch_script)
  @sch_script = stages[2].transform(@sch_script, 'phase' => phase)

  @sch_script = Saxon::XSLT(@sch_script.to_s)
end

Instance Method Details

#check(xml) ⇒ Nokogiri::XML::Document

Run schematron over xml document, returning the resulting XML

Parameters:

  • xml (Saxon::XML::Document, IO, File)

    An XML document

Returns:

  • (Nokogiri::XML::Document)

43
44
45
46
# File 'lib/schematronium.rb', line 43

def check(xml)
  xml = Saxon.XML(xml) unless xml.is_a? Saxon::XML::Document
  xml = Nokogiri::XML(@sch_script.transform(xml).to_s)
end