Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nokogiri_schematron_builder

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nokogiri_schematron_builder

  • 0.1.3
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

nokogiri_schematron_builder

Build Schematron XML documents using Nokogiri.

Installation

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

Usage

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) &gt;= 1">element "ex:A" is REQUIRED</sch:assert>
    </sch:rule>
    <sch:rule context="/ex:A">
      <sch:assert test="count(ex:B) &gt;= 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>

Development

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.

License

The 2-Clause BSD License

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/pnnl/nokogiri_schematron_builder.

FAQs

Package last updated on 07 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc