|
| 1 | +# Interpretation of XSD to MetaModel |
| 2 | + |
| 3 | +The library transforms XSD (XML Schema Definition) from XML data validation schemas to data model representations ([`MetaModel`](../internal-model.md)s). |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +XSD input processing follows this flow: |
| 10 | +1. **XML Parsing**: The XSD string is parsed into a JavaScript object using `fast-xml-parser` |
| 11 | +2. **XSD Schema Model**: The parsed XML is converted to a structured `XsdSchema` model |
| 12 | +3. **MetaModel Conversion**: The XSD types are mapped to Modelina's internal `MetaModel` representation |
| 13 | +4. **Model Generation**: The MetaModels are processed by generators to create output code |
| 14 | + |
| 15 | +## Supported XSD Features |
| 16 | + |
| 17 | +### Simple Types |
| 18 | +- **Enumerations**: `xs:restriction` with `xs:enumeration` values → enum models |
| 19 | +- **Restrictions**: Patterns, length constraints, and numeric ranges (constraint information preserved in original input) |
| 20 | + |
| 21 | +### Complex Types |
| 22 | +- **Sequences**: `xs:sequence` elements → object properties in defined order |
| 23 | +- **Choices**: `xs:choice` elements → optional object properties |
| 24 | +- **Attributes**: Converted to object properties alongside elements |
| 25 | + - `use="required"` → required properties |
| 26 | + - `use="optional"` → optional properties |
| 27 | + |
| 28 | +### Advanced Features |
| 29 | +- **Complex Content (Inheritance)**: `xs:complexContent` with `xs:extension` |
| 30 | + - Currently: Properties from base type are merged into extending type |
| 31 | + - Note: Full inheritance support may be added in future versions |
| 32 | +- **Simple Content**: Text content plus attributes → object with `value` property |
| 33 | +- **Arrays**: `maxOccurs="unbounded"` or `maxOccurs` > 1 → array properties |
| 34 | +- **Optional Elements**: `minOccurs="0"` → non-required properties |
| 35 | +- **Wildcards**: `xs:any` elements → mapped to `any` type |
| 36 | + - Supports `minOccurs` and `maxOccurs` for cardinality |
| 37 | + - Generated as properties with names like `additionalProperty`, `additionalProperty1`, etc. |
| 38 | + |
| 39 | +## Type Mappings |
| 40 | + |
| 41 | +The following table shows how XSD built-in types are mapped to Modelina's internal types: |
| 42 | + |
| 43 | +| XSD Type | Mapped Type | Notes | |
| 44 | +|----------|-------------|-------| |
| 45 | +| `xs:string`, `xs:token`, `xs:normalizedString` | string | General text types | |
| 46 | +| `xs:int`, `xs:integer`, `xs:long`, `xs:short`, `xs:byte` | integer | Integer numeric types | |
| 47 | +| `xs:unsignedLong`, `xs:unsignedInt`, `xs:unsignedShort`, `xs:unsignedByte` | integer | Unsigned integer types | |
| 48 | +| `xs:positiveInteger`, `xs:negativeInteger`, `xs:nonPositiveInteger`, `xs:nonNegativeInteger` | integer | Constrained integer types | |
| 49 | +| `xs:float`, `xs:double`, `xs:decimal` | float | Floating-point numeric types | |
| 50 | +| `xs:boolean` | boolean | Boolean type | |
| 51 | +| `xs:date`, `xs:time`, `xs:dateTime`, `xs:duration` | string | Date/time types (represented as strings) | |
| 52 | +| `xs:gYear`, `xs:gMonth`, `xs:gDay` | string | Partial date types | |
| 53 | +| `xs:anyURI`, `xs:QName` | string | URI and qualified name types | |
| 54 | +| `xs:base64Binary`, `xs:hexBinary` | string | Binary data types (represented as strings) | |
| 55 | +| Complex types | object | Converted to object models with properties | |
| 56 | +| Simple types with enumerations | enum | Converted to enum models | |
| 57 | + |
| 58 | +## Namespace Handling |
| 59 | + |
| 60 | +XSD namespaces are handled in a simplified manner: |
| 61 | +- `targetNamespace` is extracted but not actively used |
| 62 | +- Both `xs:` and `xsd:` prefixes are supported |
| 63 | +- Custom namespace prefixes (e.g., `tns:BookType`) are stripped and resolved by name only |
| 64 | + |
| 65 | +## Limitations |
| 66 | + |
| 67 | +### Partial Support |
| 68 | +- **Element References**: `ref` attribute has basic support |
| 69 | +- **Union/List**: `xs:union` and `xs:list` have basic implementations |
| 70 | +- **Inheritance**: Properties are merged rather than creating true inheritance |
| 71 | + |
| 72 | +### Not Supported |
| 73 | +- `xs:group` and `xs:attributeGroup` (named groups) |
| 74 | +- `xs:unique`, `xs:key`, `xs:keyref` (constraints) |
| 75 | +- `xs:notation` and `xs:redefine` |
| 76 | +- Full `xs:restriction` on complex types |
| 77 | +- `substitutionGroup` (element substitution) |
| 78 | +- Full namespace handling and validation |
| 79 | +- Constraint enforcement in generated models (patterns, lengths, ranges) |
| 80 | + |
| 81 | +## Examples |
| 82 | + |
| 83 | +See the [XSD to TypeScript example](../../examples/xsd-to-typescript/) for a complete working demonstration of XSD input processing. |
| 84 | + |
| 85 | +For general usage information, see the [usage documentation](../usage.md#generate-models-from-xsd-documents). |
| 86 | + |
0 commit comments