Skip to main content

Crate xsd_schema

Crate xsd_schema 

Source
Expand description

XSD 1.0 Schema Parser and Validator

This crate provides a complete XSD 1.0 schema parser with structural validation, namespace management, and W3C conformance testing. It follows the design specifications in the XSD_*.md documentation files.

§Entry Points

Use load_and_process_schema for complete processing of a single schema. XSD version is set on SchemaSet — the parser derives it automatically.

use xsd_schema::{SchemaSet, load_and_process_schema};

// Use SchemaSet::new() for XSD 1.0, SchemaSet::xsd11() for XSD 1.1
let mut schema_set = SchemaSet::new();
let xml = r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root" type="xs:string"/>
</xs:schema>"#;

let stats = load_and_process_schema(xml.as_bytes(), "schema.xsd", &mut schema_set, None)
    .expect("failed to load schema");
assert_eq!(stats.doc_id, 0);

For loading multiple schema files, use the two-phase approach:

use xsd_schema::{SchemaSet, parse_schema_only, process_loaded_schemas};

let mut schema_set = SchemaSet::new();

let schemas = [
    (r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                  targetNamespace="urn:schema1">
        <xs:element name="item1" type="xs:string"/>
    </xs:schema>"#, "schema1.xsd"),
    (r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                  targetNamespace="urn:schema2">
        <xs:element name="item2" type="xs:int"/>
    </xs:schema>"#, "schema2.xsd"),
];

// Phase 1: Parse all schemas
for (xml, uri) in schemas {
    parse_schema_only(xml.as_bytes(), uri, &mut schema_set).expect("parse failed");
}

// Phase 2: Process all schemas together
// (redefine/override application, inline assembly, reference resolution)
// Note: all participating schemas — including redefine/override targets —
// must be parsed before calling this function.
let (inline_stats, resolution_stats) = process_loaded_schemas(&mut schema_set)
    .expect("processing failed");

§Advanced: Low-Level Parser

For custom pipelines, the low-level parser is available at parser::parse_schema. This only performs Phase 1 (parsing + assembly) - subsequent phases must be run manually.

§Architecture

The parser uses a state machine approach with typed parser frames for each XSD element type. All schema components are stored in arenas with typed IDs to avoid reference cycles.

§Core Modules

  • parser - XSD document parsing with location tracking
  • namespace - String interning and namespace management
  • schema - Schema component model (elements, types, groups)
  • types - Type definitions and facets

§Example

use xsd_schema::SchemaSet;

// Create an empty schema set
let mut schema_set = SchemaSet::new();

// Schema parser will be added in later implementation
// For now, the schema set can be populated programmatically

Re-exports§

pub use error::FacetError;
pub use error::FacetResult;
pub use error::SchemaError;
pub use error::SchemaResult;
pub use schema::SchemaDocument;
pub use schema::SchemaSet;
pub use schema::assemble_inline_types;
pub use schema::resolve_all_references;
pub use schema::InlineAssemblyStats;
pub use schema::ResolutionStats;
pub use schema::model::XsdVersion;
pub use schema::model::RegexCompat;
pub use types::BuiltinTypes;
pub use types::PrimitiveTypeCode;
pub use types::ValueKind;
pub use types::XmlTypeCode;
pub use types::facet_applicable;
pub use types::facet_applicable_for_type;
pub use types::normalize_whitespace;
pub use types::FacetApplicability;
pub use types::FacetFixed;
pub use types::FacetKind;
pub use types::FacetSet;
pub use types::WhitespaceMode;
pub use navigator::DomNavigator;
pub use navigator::DomNodeType;
pub use navigator::NamespaceAxisScope;
pub use navigator::NavigatorError;
pub use navigator::RoXmlNavigator;
pub use navigator::XmlNodeOrder;
pub use xpath::BufferedNodeIterator;
pub use xpath::EmptyIterator;
pub use xpath::EvalValue;
pub use xpath::ExternalVar;
pub use xpath::RangeIterator;
pub use xpath::TreeComparer;
pub use xpath::TypedEvaluator;
pub use xpath::VecNodeIterator;
pub use xpath::XPathContext;
pub use xpath::XPathEvaluator;
pub use xpath::XPathExpr;
pub use xpath::XmlItem;
pub use xpath::XmlItemRef;
pub use xpath::XmlNodeIterator;
pub use pipeline::load_and_process_schema;
pub use pipeline::load_schema;
pub use pipeline::parse_schema_only;
pub use pipeline::process_loaded_schemas;
pub use pipeline::DirectiveStats;
pub use pipeline::PipelineConfig;
pub use pipeline::PipelineStats;
pub use builder::CompilationStats;
pub use builder::CompiledSchemaSet;
pub use builder::SchemaSetBuilder;
pub use parser::resolver::decode_xml_bytes;
pub use parser::resolver::decode_xml_to_utf8_bytes;
pub use parser::resolver::EmbeddedLoader;
pub use parser::resolver::FileSystemLoader;
pub use parser::resolver::LoaderChain;
pub use parser::resolver::ResolverConfig;
pub use parser::resolver::SchemaCatalog;
pub use parser::resolver::SchemaLoader;
pub use parser::resolver::SchemaResolver;
pub use embedded::get_embedded_schema;
pub use embedded::has_embedded_schema;
pub use embedded::XML_NAMESPACE;
pub use embedded::XML_XSD;
pub use compiler::compile_model_group;
pub use compiler::compile_particle;
pub use compiler::fragment_to_table;
pub use compiler::CompileContext;
pub use compiler::FragmentBuilder;
pub use compiler::NfaCompileError;
pub use compiler::NfaCompileResult;
pub use compiler::NfaFragment;
pub use compiler::NfaState;
pub use compiler::NfaTable;
pub use compiler::NfaTerm;
pub use compiler::NfaTransition;
pub use compiler::StateId;
pub use compiler::TransitionKind;
pub use validation::error as validation_error;
pub use validation::error_with_path as validation_error_with_path;
pub use validation::facet_constraint_code;
pub use validation::from_facet_error;
pub use validation::from_value_error;
pub use validation::value_error_constraint_code;
pub use validation::ValidationError as InstanceValidationError;
pub use validation::ValidationResult as InstanceValidationResult;
pub use validation::hint_loader::enrich_schema_set;
pub use validation::hint_loader::load_hints_into_builder;
pub use validation::hint_loader::EnrichmentOutcome;
pub use validation::hint_loader::HintLoadResult;
pub use validation::info::NoNamespaceSchemaLocationHint;
pub use validation::info::SchemaLocationHint;
pub use ids::*;

Modules§

arenas
Arena storage for schema components
builder
SchemaSet builder with compile() pattern
compiler
NFA compilation for XSD content models
document
Page-based XML document buffer for XPath 2.0 evaluation.
embedded
Embedded schema assets
error
Error types for XSD parsing and validation
ids
Typed IDs for arena-based storage
namespace
Namespace management and string interning
navigator
DOM navigation trait and types
parser
XSD document parsing
pipeline
Schema processing pipeline
regex_convert
XML Schema / XPath 2.0 regex pattern conversion.
schema
Schema component model
types
XSD type definitions and facets
validation
Instance validation against XSD schemas
xpath
XPath2 evaluation engine