Leptris — schema surfaces (engine v1.9.429)
libleptris ships four schema surfaces — XSD, RelaxNG, Schematron, and DTD — behind one set of capability flags, so a build declares what it carries and the bindings expose the faces the engine has. This page documents each with snippets executed against the shipped gem.
Capability flags — the documented probe
Schema surfaces are compile-time features; a build declares what it carries through the LEPTRIS_CAP_* flags (LEPTRIS_CAP_XSD, LEPTRIS_CAP_XSD_SET,LEPTRIS_CAP_RELAXNG, LEPTRIS_CAP_SCHEMATRON,LEPTRIS_CAP_DTD). The Ruby bindings surface the same probe — check before reaching for a face on a slim build:
require "leptris"
require "leptris/xml"
Leptris::XML::XSD.compile(xsd_text) # raises a build error on a slim buildXSD — the compile face
Compile once from text, a file, or a multi-document set (leptris_xsd_compile, leptris_xsd_compile_file,leptris_xsd_compile_set); the schema handle is error-carrying (leptris_xsd_error names the last compile problem on an invalid handle) and reports its top-level declaration count:
#include <stdio.h>
#include <string.h>
#include <leptris.h>
const char *xsd =
"<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
"<xs:element name=\"note\" type=\"NoteType\"/>"
"<xs:complexType name=\"NoteType\"><xs:sequence>"
"<xs:element name=\"to\" type=\"xs:string\"/>"
"<xs:element name=\"from\" type=\"xs:string\"/>"
"</xs:sequence></xs:complexType></xs:schema>";
LeptrisStatus st = 0;
LeptrisXsdSchema schema = leptris_xsd_compile(xsd, strlen(xsd), &st);
if (schema == NULL) {
printf("compile failed: %s\n", leptris_xsd_error(NULL));
return 1;
}
printf("declarations: %ld\n", (long)leptris_xsd_declaration_count(schema));
leptris_xsd_free(schema);
/* declarations: 2 */The same face in Ruby — Leptris::XML::XSD (autoloaded from leptris/xml; compile,compile_file, compile_set):
require "leptris"
require "leptris/xml"
schema = Leptris::XML::XSD.compile(<<~XSD)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note" type="NoteType"/>
<xs:complexType name="NoteType">
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
XSD
schema.class # => Leptris::XML::XSD::Schema
schema.declaration_count # => 2
doc = Leptris::XML.parse('<note><to>you</to><from>me</from></note>')
schema.valid?(doc) # => true
schema.valid?(Leptris::XML.parse('<note/>'))
# => false (missing required children)
schema.validate_errors(Leptris::XML.parse('<note><to>x</to></note>'))
# => ["element 'note' @1:7: element 'note': children do not match the content model"]LeptrisStatus st = 0;
LeptrisDocument doc = leptris_parse_string("<note><to>x</to></note>", 23, &st);
if (leptris_xsd_validate(schema, doc) == 0)
for (size_t i = 0; i < leptris_xsd_error_count(schema); i++)
printf("%s\n", leptris_xsd_error_at(schema, i));
// => element 'note' @1:7: element 'note': children do not match the content model
leptris_document_free(doc);RelaxNG
Parse a RelaxNG grammar (Leptris::XML::RelaxNG.parse /parse_file; the C face is theleptris_relaxng_* family, capabilityLEPTRIS_CAP_RELAXNG):
require "leptris"
require "leptris/xml"
rng = Leptris::XML::RelaxNG.parse(<<~RNG)
<element name="note" xmlns="http://relaxng.org/ns/structure/1.0">
<element name="to"><text/></element>
</element>
RNG
rng.class # => Leptris::XML::RelaxNG::SchemaSchematron
Parse a Schematron pattern sheet (Leptris::XML::Schematron.parse /parse_file; the C face carriesleptris_schematron_parse,_parse_phase (phase selection),_valid, and the error handback vialeptris_schematron_error /leptris_last_error; capabilityLEPTRIS_CAP_SCHEMATRON; the FFI attach lives in the binding, engine 1.9.126+):
require "leptris"
require "leptris/xml"
sch = Leptris::XML::Schematron.parse(<<~SCH)
<schema xmlns="http://purl.oclc.org/dsdl/schematron">
<pattern>
<rule context="note">
<assert test="to">needs a to</assert>
</rule>
</pattern>
</schema>
SCH
sch.class # => Leptris::XML::Schematron::SchemaDTD
The DTD surface rides src/include/leptris/dtd.h — internal/external subset parsing over the same engine; the binding exposes it through the same autoload pattern as the faces above.
This is the curated guide. The repositories are canonical: when this page and the repo disagree, the repo wins. Full documentation lives with the source.