Build Schematron XML documents using Nokogiri.
Add this line to your application's Gemfile:
gem "nokogiri_schematron_builder"
And then execute:
$ bundle
Or install it yourself as:
$ gem install nokogiri_schematron_builder
Add this line to your application:
require "nokogiri/xml/schematron/schema"
Create the schema using the domain-specific language:
schema = Nokogiri::XML::Schematron::Schema.new(title: "Example schema") do
ns(prefix: "ex", uri: "http://example.com/ns#")
pattern(title: "Example pattern") do
rule(context: "/") do
assert(test: "count(ex:A) >= 1", message: "element \"ex:A\" is REQUIRED")
end
rule(context: "/ex:A") do
assert(test: "count(ex:B) >= 0", message: "element \"ex:B\" is OPTIONAL")
end
rule(context: "/ex:A/ex:B") do
assert(test: "not(ex:C)", message: "element \"ex:C\" is NOT RECOMMENDED")
end
end
end
Or, equivalently:
schema = Nokogiri::XML::Schematron::Schema.new(title: "Example schema") do
ns(prefix: "ex", uri: "http://example.com/ns#")
pattern(title: "Example pattern") do
context("/") do
require("ex:A") do
permit("ex:B") do
reject("ex:C")
end
end
end
end
end
Next, construct a Nokogiri::XML::Builder
object:
builder = schema.to_builder(encoding: "UTF-8")
Finally, generate the XML:
xml = builder.to_xml
The result is:
<?xml version="1.0" encoding="UTF-8"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron">
<sch:title>Example schema</sch:title>
<sch:ns prefix="ex" uri="http://example.com/ns#"/>
<sch:pattern>
<sch:title>Example pattern</sch:title>
<sch:rule context="/">
<sch:assert test="count(ex:A) >= 1">element "ex:A" is REQUIRED</sch:assert>
</sch:rule>
<sch:rule context="/ex:A">
<sch:assert test="count(ex:B) >= 0">element "ex:B" is OPTIONAL</sch:assert>
</sch:rule>
<sch:rule context="/ex:A/ex:B">
<sch:assert test="not(ex:C)">element "ex:C" is NOT RECOMMENDED</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>
After checking out the repo, run bin/setup
to install dependencies. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/pnnl/nokogiri_schematron_builder.