- General
- Entry point
- Parsing options
- Schema Mappings
- Validation
- Type Resolution
- Schema class reference
- Element class reference
- ComplexType class reference
- SimpleType class reference
- Restriction facets
- Content model classes
- Attribute class reference
- Import and Include classes
- Validation
- Liquid template helpers
- Limitations
- Examples
- JSON/YAML Schema Import
- See Also
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.
| There are two XSD-related features in lutaml-model: |
-
XSD Schema Parsing - Parse XSD schemas into Ruby objects for inspection and manipulation. See XSD Schema Parsing Reference.
-
Schema Import (this section) - Generate Ruby model classes from XSD schemas that can be used for serialization.
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 |
require 'lutaml/xml/schema/xsd'
# Parse from a string
xsd_content = File.read('schema.xsd')
schema = Lutaml::Xml::Schema::Xsd.parse(xsd_content)
# Parse from a file path (for resolving includes/imports)
schema = Lutaml::Xml::Schema::Xsd.parse(
File.read('schema.xsd'),
location: 'schema.xsd'
)
# Access parsed elements
puts "Elements: #{schema.element.size}"
puts "Complex types: #{schema.complex_type.size}"
puts "Simple types: #{schema.simple_type.size}"Schema Mappings
When working with schemas that import or include other schemas, you can provide local file mappings to avoid network requests:
schema = Lutaml::Xml::Schema::Xsd.parse(
File.read('main.xsd'),
location: 'main.xsd',
schema_mappings: {
'http://example.com/schemas/types.xsd' => './local/types.xsd',
'http://example.com/schemas/common.xsd' => './local/common.xsd'
}
)Validation
The XSD module includes schema validation capabilities.
Validate Schema Structure
Validate an XSD schema before parsing:
# Validation is performed by default during parsing
schema = Lutaml::Xml::Schema::Xsd.parse(xsd_content, validate_schema: true)
# Manual validation
validator = Lutaml::Xml::Schema::Xsd::SchemaValidator.new(version: '1.0')
result = validator.validate(xsd_content)
puts "Schema is valid"
# Detect XSD version
version = Lutaml::Xml::Schema::Xsd::SchemaValidator.detect_version(xsd_content)
puts "Detected XSD version: #{version}" # => "1.0" or "1.1"The validator checks:
-
XML syntax validity
-
Root element is
xs:schema -
Correct XML Schema namespace
-
Version compatibility (XSD 1.0 vs 1.1 features)
Type Resolution
The XSD module integrates with Lutaml::Model’s type system for resolving XSD types to Ruby classes.
Schema class reference
The Lutaml::Xml::Schema::Xsd::Schema class represents an XSD schema element.
schema = Lutaml::Xml::Schema::Xsd.parse(xsd_content)
# Access schema properties
schema.target_namespace # Target namespace URI
schema.element_form_default # Element form default (:qualified, :unqualified)
schema.attribute_form_default # Attribute form default
schema.version # Schema version
# Access schema components
schema.element # Array of Element objects
schema.complex_type # Array of ComplexType objects
schema.simple_type # Array of SimpleType objects
schema.attribute # Array of Attribute objects
schema.attribute_group # Array of AttributeGroup objects
schema.group # Array of Group objects
schema.import # Array of Import objects
schema.include # Array of Include objects
# Helper methods
schema.find_type('PersonType') # Find type by name
schema.find_complex_type('PersonType') # Find complex type by name
schema.find_simple_type('StatusType') # Find simple type by name
schema.find_element('person') # Find element by name
schema.stats # Hash with counts of all components
schema.summary # Human-readable summary stringAttributes
| 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.
element = schema.element.first
element.name # Element name
element.type # Type reference (e.g., "xs:string", "tns:PersonType")
element.ref # Reference to another element
element.min_occurs # Minimum occurrences (default: 1)
element.max_occurs # Maximum occurrences (default: 1, or "unbounded")
element.default # Default value
element.fixed # Fixed value
element.nillable # Whether element can be nil
element.substitution_group # Substitution group reference
element.complex_type # Inline complex type
element.simple_type # Inline simple typeAttributes
| 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.
complex_type = schema.complex_type.first
complex_type.name # Type name
complex_type.mixed # Whether mixed content is allowed
complex_type.abstract # Whether type is abstract
complex_type.block # Block attribute
complex_type.final # Final attribute
# Content model
complex_type.sequence # Sequence particle
complex_type.choice # Choice particle
complex_type.all # All particle
complex_type.group # Group reference
complex_type.complex_content # Complex content extension/restriction
complex_type.simple_content # Simple content extension/restriction
# Attributes
complex_type.attribute # Array of Attribute objects
# Convenience method to get elements from content model
complex_type.elements # Elements from sequence/choice/allAttributes
| 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.
simple_type = schema.simple_type.first
simple_type.name # Type name
simple_type.final # Final attribute
# Type definition
simple_type.restriction # Restriction facet
simple_type.list # List facet
simple_type.union # Union facetRestriction facets
Restriction facets define constraints on simple types:
restriction = simple_type.restriction
restriction.base # Base type
restriction.min_inclusive # Minimum inclusive value
restriction.max_inclusive # Maximum inclusive value
restriction.min_exclusive # Minimum exclusive value
restriction.max_exclusive # Maximum exclusive value
restriction.min_length # Minimum length
restriction.max_length # Maximum length
restriction.length # Exact length
restriction.pattern # Pattern constraint
restriction.enumeration # Enumeration values
restriction.white_space # Whitespace handling
restriction.total_digits # Total digits
restriction.fraction_digits # Fraction digitsContent 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)
Liquid template helpers
Parsed XSD objects expose helper methods designed for use with Liquid templates (see Liquid Templates). These helpers resolve cross-references, flatten nested content models, and provide sorted access to schema components.
| Method | Description |
|---|---|
| Global elements sorted alphabetically by name. |
| Complex type definitions sorted by name. |
| Attribute group definitions sorted by name. |
| Simple type definitions sorted by name. |
| Global attribute declarations sorted by name. |
| Complex types whose content model references this element. |
| Attributes from the element’s referenced complex type. |
| Child elements from the element’s referenced complex type. |
| Resolved type name after following |
| Effective element name after resolving |
| The resolved XSD object behind a |
| Complex type referenced by the element’s |
| Elements (root-level and nested) that reference this complex type. |
| Flattened attributes including those from attribute groups and extensions. |
| All nested element declarations from sequences, choices, and groups. |
| Direct child elements, excluding attributes, attribute groups, and annotations. |
|
|
| Resolved type name after following |
| Effective attribute name after resolving |
| Complex types that reference this attribute group. |
| Flattened list of attributes from the group and nested groups. |
| Recursively collected child elements. |
| Recursively collected child elements. |
| Child elements from the resolved group definition. |
| Attributes inherited from the base type plus extension attributes. |
| Base type resolved from inline, extension, or restriction declarations. |
| Method | Description |
|---|---|
| Elements in document order, with references resolved. |
| Elements whose |
| Content model type predicates. |
| Content model type predicates. |
| Occurrence bounds as integers. |
| Namespace prefix of the containing schema. |
| Pretty-printed XML output. |
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.
Examples
Basic Schema Parsing
require 'lutaml/xml/schema/xsd'
# Parse a schema
xsd = <<~XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.com/person"
xmlns:tns="http://example.com/person">
<xs:element name="person" type="tns:PersonType"/>
<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="email" type="xs:string" minOccurs="0"/>
<xs:element name="age" type="xs:integer"/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use="required"/>
</xs:complexType>
</xs:schema>
XSD
schema = Lutaml::Xml::Schema::Xsd.parse(xsd)
# Access elements
person_element = schema.element.first
puts "Element: #{person_element.name}" # => "person"
# Access complex types
person_type = schema.complex_type.first
puts "Type: #{person_type.name}" # => "PersonType"
# Access type's sequence elements
person_type.sequence.element.each do |elem|
puts " - #{elem.name}: #{elem.type}"
end
# Output:
# - name: xs:string
# - email: xs:string
# - age: xs:integerSchema with Imports
require 'lutaml/xml/schema/xsd'
# Main schema with imports
main_xsd = File.read('schemas/main.xsd')
schema = Lutaml::Xml::Schema::Xsd.parse(
main_xsd,
location: 'schemas/main.xsd',
schema_mappings: {
'http://example.com/schemas/types.xsd' => 'schemas/types.xsd',
'http://example.com/schemas/common.xsd' => 'schemas/common.xsd'
}
)
# All imported types are available
puts "Total types: #{schema.complex_type.size + schema.simple_type.size}"Supported XSD Elements
| XSD Element | Ruby Class | Key Attributes |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| - |
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