schematron 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +14 -0
- data/bin/validate +28 -0
- data/lib/schematron.rb +67 -0
- data/schematron.gemspec +12 -0
- metadata +56 -0
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
require 'spec/rake/spectask'
|
4
|
+
|
5
|
+
Spec::Rake::SpecTask.new('spec') do |t|
|
6
|
+
t.libs << 'lib'
|
7
|
+
t.libs << 'spec'
|
8
|
+
t.spec_opts << "--color"
|
9
|
+
# t.warning = true
|
10
|
+
# t.rcov = true
|
11
|
+
# t.rcov_opts += ["-x /Library", "-x spec"]
|
12
|
+
end
|
13
|
+
|
14
|
+
task :default => [:spec]
|
data/bin/validate
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require 'libxml'
|
4
|
+
require 'schematron'
|
5
|
+
|
6
|
+
include LibXML
|
7
|
+
|
8
|
+
if ARGV.size != 2
|
9
|
+
$stderr.puts "usage: validate [schematron] [instance]"
|
10
|
+
exit 1
|
11
|
+
end
|
12
|
+
|
13
|
+
# use the line numbers
|
14
|
+
XML.default_line_numbers = true
|
15
|
+
|
16
|
+
# Get sch and xml from command line
|
17
|
+
schema_doc = XML::Document.file ARGV[0]
|
18
|
+
instance_doc = XML::Document.file ARGV[1]
|
19
|
+
|
20
|
+
stron = Schematron::Schema.new schema_doc
|
21
|
+
stron.validate(instance_doc).each do |error|
|
22
|
+
puts '%s "%s" on line %d: %s' % [
|
23
|
+
error[:type],
|
24
|
+
error[:name],
|
25
|
+
error[:line],
|
26
|
+
error[:message]
|
27
|
+
]
|
28
|
+
end
|
data/lib/schematron.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'libxml'
|
2
|
+
require 'libxslt'
|
3
|
+
|
4
|
+
module Schematron
|
5
|
+
|
6
|
+
include LibXML
|
7
|
+
include LibXSLT
|
8
|
+
|
9
|
+
# The location of the ISO schematron implemtation lives
|
10
|
+
ISO_IMPL_DIR = 'iso_impl'
|
11
|
+
|
12
|
+
# The file names of the compilation stages
|
13
|
+
ISO_FILES = [ 'iso_dsdl_include.xsl',
|
14
|
+
'iso_abstract_expand.xsl',
|
15
|
+
'iso_svrl.xsl' ]
|
16
|
+
|
17
|
+
# Namespace prefix declarations for use in XPaths
|
18
|
+
NS_PREFIXES = {
|
19
|
+
'svrl' => 'http://purl.oclc.org/dsdl/svrl'
|
20
|
+
}
|
21
|
+
|
22
|
+
class Schema
|
23
|
+
|
24
|
+
def initialize(doc)
|
25
|
+
@schema_doc = doc
|
26
|
+
end
|
27
|
+
|
28
|
+
def validate(instance_doc)
|
29
|
+
|
30
|
+
# Compile schematron into xsl that maps to svrl
|
31
|
+
xforms = ISO_FILES.map do |file|
|
32
|
+
|
33
|
+
Dir.chdir(ISO_IMPL_DIR) do
|
34
|
+
doc = XML::Document.file file
|
35
|
+
LibXSLT::XSLT::Stylesheet.new doc
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
validator_doc = xforms.inject(@schema_doc) { |xml, xsl| xsl.apply xml }
|
41
|
+
validator_xsl = LibXSLT::XSLT::Stylesheet.new validator_doc
|
42
|
+
|
43
|
+
# Validate the xml
|
44
|
+
results_doc = validator_xsl.apply instance_doc
|
45
|
+
|
46
|
+
# compile the errors
|
47
|
+
results = []
|
48
|
+
results_doc.root.find('//svrl:failed-assert', NS_PREFIXES).each do |assert|
|
49
|
+
context = instance_doc.root.find_first assert['location']
|
50
|
+
|
51
|
+
assert.find('svrl:text/text()', NS_PREFIXES).each do |message|
|
52
|
+
results << {
|
53
|
+
:type => context.node_type_name,
|
54
|
+
:name => context.name,
|
55
|
+
:line => context.line_num,
|
56
|
+
:message => message.content.strip
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
results
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
data/schematron.gemspec
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "schematron"
|
3
|
+
spec.version = '0.0.0'
|
4
|
+
spec.summary = "ISO Schematron Validation in ruby"
|
5
|
+
spec.email = "flazzarino@gmail.com"
|
6
|
+
spec.homepage = 'http://github.com/flazz/iso-schematron'
|
7
|
+
rubyforge_project = "schematron"
|
8
|
+
spec.authors = ["Francesco Lazzarino"]
|
9
|
+
spec.files = ["Rakefile", "schematron.gemspec",
|
10
|
+
"bin/validate", "lib/schematron.rb"]
|
11
|
+
spec.has_rdoc = true
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: schematron
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Francesco Lazzarino
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-13 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: flazzarino@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- schematron.gemspec
|
27
|
+
- bin/validate
|
28
|
+
- lib/schematron.rb
|
29
|
+
has_rdoc: true
|
30
|
+
homepage: http://github.com/flazz/iso-schematron
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.3.1
|
52
|
+
signing_key:
|
53
|
+
specification_version: 2
|
54
|
+
summary: ISO Schematron Validation in ruby
|
55
|
+
test_files: []
|
56
|
+
|