General
The Lutaml::Xml::Schema::Xsd module provides functionality to parse XSD (XML Schema Definition) schemas into Ruby objects for inspection, manipulation, and round-tripping. This allows you to work with XSD schemas programmatically without generating Ruby model classes.
The XSD parsing functionality is available in the Lutaml::Xml::Schema::Xsd namespace and provides a complete object model for XSD schema constructs.
Key use cases
-
Schema inspection and documentation generation
-
Schema validation before processing
-
Schema transformation and manipulation
-
Round-tripping XSD content (parse, modify, serialize)
-
Programmatic access to schema components
Feature distinction
| This feature is distinct from Schema import which generates Ruby model classes from XSD schemas. XSD Schema Parsing is for working with XSD schemas as objects. |
Entry point
Parsing options
The parse method accepts the following options:
schema = Lutaml::Xml::Schema::Xsd.parse(
xsd,
location: base_path,
schema_mappings: mappings,
validate_schema: true
)| Option | Description |
|---|---|
| Base path or URL for resolving relative paths in |
| Array of |
| When |
| Internal flag used when recursively parsing included/imported schemas. Should not be set by users. |
| Custom type register to use for parsing. Defaults to the built-in |
Schema class reference
The Lutaml::Xml::Schema::Xsd::Schema class represents an XSD schema element.
Attributes
| Attribute | Type | Description |
|---|---|---|
|
| Schema ID |
|
| Schema version |
|
| Target namespace URI |
|
| Element form default (qualified/unqualified) |
|
| Attribute form default (qualified/unqualified) |
|
| Default final attribute |
|
| Default block attribute |
Collections
| Collection | Type | Description |
|---|---|---|
|
| Global element declarations |
|
| Complex type definitions |
|
| Simple type definitions |
|
| Global attribute declarations |
|
| Attribute group definitions |
|
| Named model groups |
|
| Schema imports |
|
| Schema includes |
|
| Schema annotations |
Methods
| Method | Description |
|---|---|
| Find a type definition by local name (searches both simple and complex types) |
| Find a complex type definition by name |
| Find a simple type definition by name |
| Find a global element declaration by name |
| Returns a hash with counts of all schema components |
| Returns a human-readable summary string |
| Basic validation check (has target namespace) |
| Schema name derived from target namespace |
| Serialize schema back to XML string |
Example usage
require 'lutaml/xml/schema/xsd'
schema = Lutaml::Xml::Schema::Xsd.parse(xsd_content)
# Access properties
puts schema.target_namespace
puts schema.element_form_default
# Access collections
schema.complex_type.each do |type|
puts "Complex type: #{type.name}"
end
# Find specific components
type = schema.find_type("AddressType")
elem = schema.find_element("Address")
# Get statistics
puts schema.stats
# => {:elements=>1, :complex_types=>1, :simple_types=>0, :attributes=>0,
# :groups=>0, :attribute_groups=>0, :imports=>0, :includes=>0, :namespaces=>1}
# Get summary
puts schema.summary
# => "http://example.com/test: 1 elements, 1 complex types, 0 simple types"
# Round-trip
xml_output = schema.to_xmlElement class reference
The Lutaml::Xml::Schema::Xsd::Element class represents an XSD element declaration.
Attributes
| Attribute | Type | Description |
|---|---|---|
|
| Element name |
|
| Type reference (e.g., "xs:string", "tns:PersonType") |
|
| Reference to another element |
|
| Minimum occurrences (default: "1") |
|
| Maximum occurrences (default: "1", or "unbounded") |
|
| Default value |
|
| Fixed value |
|
| Whether element can be nil |
|
| Form (qualified/unqualified) |
|
| Block attribute |
|
| Final attribute |
|
| Whether element is abstract |
|
| Substitution group reference |
|
| Element annotation |
|
| Inline simple type |
|
| Inline complex type |
ComplexType class reference
The Lutaml::Xml::Schema::Xsd::ComplexType class represents an XSD complex type.
Attributes
| Attribute | Type | Description |
|---|---|---|
|
| Type name |
|
| Whether mixed content is allowed |
|
| Whether type is abstract |
|
| Block attribute |
|
| Final attribute |
|
| Sequence content model |
|
| Choice content model |
|
| All content model |
|
| Group reference |
|
| Complex content extension/restriction |
|
| Simple content extension/restriction |
|
| Attribute declarations |
|
| Attribute group references |
SimpleType class reference
The Lutaml::Xml::Schema::Xsd::SimpleType class represents an XSD simple type.
Content model classes
Sequence
The Sequence class represents an XSD sequence compositor.
| Attribute | Type | Description |
|---|---|---|
|
| Minimum occurrences |
|
| Maximum occurrences |
|
| Child elements |
|
| Nested choices |
|
| Nested sequences |
|
| Group references |
|
| Any elements |
Choice
The Choice class represents an XSD choice compositor.
| Attribute | Type | Description |
|---|---|---|
|
| Minimum occurrences |
|
| Maximum occurrences |
|
| Child elements |
|
| Nested choices |
|
| Nested sequences |
|
| Group references |
|
| Any elements |
All
The All class represents an XSD all compositor.
| Attribute | Type | Description |
|---|---|---|
|
| Minimum occurrences |
|
| Maximum occurrences |
|
| Child elements |
Group
The Group class represents an XSD group definition or reference.
| Attribute | Type | Description |
|---|---|---|
|
| Group name (for definitions) |
|
| Group reference (for references) |
|
| Minimum occurrences |
|
| Maximum occurrences |
|
| Sequence content |
|
| Choice content |
|
| All content |
Attribute class reference
The Lutaml::Xml::Schema::Xsd::Attribute class represents an XSD attribute.
Attributes
| Attribute | Type | Description |
|---|---|---|
|
| Attribute name |
|
| Type reference |
|
| Reference to another attribute |
|
| Usage: "required", "optional", or "prohibited" (default: "optional") |
|
| Default value |
|
| Fixed value |
|
| Form (qualified/unqualified) |
|
| Attribute annotation |
|
| Inline simple type |
Import and Include classes
Validation
SchemaValidator class
The Lutaml::Xml::Schema::Xsd::SchemaValidator class validates XSD schemas before parsing.
Constructor
validator = Lutaml::Xml::Schema::Xsd::SchemaValidator.new(version: "1.0")The version parameter specifies the XSD version to validate against ("1.0" or "1.1").
Methods
| Method | Description |
|---|---|
| Validate XSD content. Returns |
| Class method. Detects XSD version from content, returns "1.0" or "1.1". |
Example
require 'lutaml/xml/schema/xsd'
xsd_content = File.read('schema.xsd')
# Detect version
version = Lutaml::Xml::Schema::Xsd::SchemaValidator.detect_version(xsd_content)
puts "Detected version: #{version}"
# Validate
validator = Lutaml::Xml::Schema::Xsd::SchemaValidator.new(version: version)
begin
validator.validate(xsd_content)
puts "Schema is valid"
rescue Lutaml::Xml::Schema::Xsd::SchemaValidationError => e
puts "Validation failed: #{e.message}"
endValidation checks
The validator performs the following checks:
-
XML syntax validity
-
Root element is
xs:schema -
Correct XML Schema namespace (
http://www.w3.org/2001/XMLSchema) -
Version compatibility (XSD 1.0 vs 1.1 features)
Limitations
The XSD Schema Parsing feature has the following limitations:
-
No CLI interface - This is a programmatic API only. Use it within Ruby applications.
-
No schema bundler - The
SchemaRepositoryfunctionality from lutaml-xsd is not included. Uselocationandschema_mappingsfor complex schemas. -
No HTML/SPA generation - Documentation generation features are not included.
-
No formatters - Template-based output formatting is not available.
See Also
-
Schema Import - Generate Ruby model classes from XSD
-
XSD Generation - Generate XSD from LutaML models