Interpretation of XSD to MetaModel

The library transforms XSD (XML Schema Definition) from XML data validation schemas to data model representations (MetaModels).

The algorithm processes the XSD document structure to create models that represent the data types defined in the schema, including complex types, simple types, elements, attributes, and their relationships.

Overview

XSD input processing follows this flow:

  1. XML Parsing: The XSD string is parsed into a JavaScript object using fast-xml-parser
  2. XSD Schema Model: The parsed XML is converted to a structured XsdSchema model
  3. MetaModel Conversion: The XSD types are mapped to Modelina's internal MetaModel representation
  4. Model Generation: The MetaModels are processed by generators to create output code

Supported XSD Features

Simple Types

  • Enumerations: xs:restriction with xs:enumeration values → enum models
  • Restrictions: Patterns, length constraints, and numeric ranges (constraint information preserved in original input)

Complex Types

  • Sequences: xs:sequence elements → object properties in defined order
  • Choices: xs:choice elements → optional object properties
  • Attributes: Converted to object properties alongside elements
    • use="required" → required properties
    • use="optional" → optional properties

Advanced Features

  • Complex Content (Inheritance): xs:complexContent with xs:extension
    • Currently: Properties from base type are merged into extending type
    • Note: Full inheritance support may be added in future versions
  • Simple Content: Text content plus attributes → object with value property
  • Arrays: maxOccurs="unbounded" or maxOccurs > 1 → array properties
  • Optional Elements: minOccurs="0" → non-required properties
  • Wildcards: xs:any elements → mapped to any type
    • Supports minOccurs and maxOccurs for cardinality
    • Generated as properties with names like additionalProperty, additionalProperty1, etc.

Type Mappings

The following table shows how XSD built-in types are mapped to Modelina's internal types:

XSD TypeMapped TypeNotes
xs:string, xs:token, xs:normalizedStringstringGeneral text types
xs:int, xs:integer, xs:long, xs:short, xs:byteintegerInteger numeric types
xs:unsignedLong, xs:unsignedInt, xs:unsignedShort, xs:unsignedByteintegerUnsigned integer types
xs:positiveInteger, xs:negativeInteger, xs:nonPositiveInteger, xs:nonNegativeIntegerintegerConstrained integer types
xs:float, xs:double, xs:decimalfloatFloating-point numeric types
xs:booleanbooleanBoolean type
xs:date, xs:time, xs:dateTime, xs:durationstringDate/time types (represented as strings)
xs:gYear, xs:gMonth, xs:gDaystringPartial date types
xs:anyURI, xs:QNamestringURI and qualified name types
xs:base64Binary, xs:hexBinarystringBinary data types (represented as strings)
Complex typesobjectConverted to object models with properties
Simple types with enumerationsenumConverted to enum models

Namespace Handling

XSD namespaces are handled in a simplified manner:

  • targetNamespace is extracted but not actively used
  • Both xs: and xsd: prefixes are supported
  • Custom namespace prefixes (e.g., tns:BookType) are stripped and resolved by name only

Limitations

Partial Support

  • Element References: ref attribute has basic support
  • Union/List: xs:union and xs:list have basic implementations
  • Inheritance: Properties are merged rather than creating true inheritance

Not Supported

  • xs:group and xs:attributeGroup (named groups)
  • xs:unique, xs:key, xs:keyref (constraints)
  • xs:notation and xs:redefine
  • Full xs:restriction on complex types
  • substitutionGroup (element substitution)
  • Full namespace handling and validation
  • Constraint enforcement in generated models (patterns, lengths, ranges)

Examples

See the XSD to TypeScript example for a complete working demonstration of XSD input processing.

For general usage information, see the usage documentation.