Skip to main content

Export: XML Schema

Converts the data contract to an XML Schema (XSD) with one global element per schema.

datacontract export xsd orders.odcs.yaml --output orders.xsd

Running this against the example orders contract produces (excerpt):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="orders">
<xs:annotation>
<xs:documentation>One row per customer order.</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="order_id" type="xs:string">
<xs:annotation>
<xs:documentation>Unique identifier of the order.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="order_timestamp" type="xs:dateTime">
<xs:annotation>
<xs:documentation>Timestamp when the order was placed.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="customer_id" type="xs:string">
<xs:annotation>
<xs:documentation>Reference to the customer who placed the order.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="order_total" type="xs:integer">
<xs:annotation>
<xs:documentation>Total amount of the order in cents.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="status" type="xs:string">
<xs:annotation>
<xs:documentation>Current fulfilment status of the order.</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="line_items">
<xs:annotation>
<xs:documentation>One row per line item within an order.</xs:documentation>
</xs:annotation>
…
  • Objects become nested complex types, arrays become elements that repeat (minItems/maxItems set minOccurs/maxOccurs), and a property that is not required gets minOccurs="0".
  • The logicalType sets the XSD type (timestamp → xs:dateTime, number → xs:decimal, …), unless the physicalType names a built-in XSD type of the same logical type, such as positiveInteger or token, or the ODCS format gives a size: i8 → xs:byte, i16 → xs:short, i32 → xs:int, i64 → xs:long, u8 to u64 → xs:unsignedByte to xs:unsignedLong, f32 → xs:float, f64 → xs:double.
  • enum, pattern, minLength/maxLength, minimum/maximum (and their exclusive forms), and the precision/scale of decimals become facets. A bound that only repeats the range of the XSD type, such as minimum: 0 on an unsignedInt, is left out. XSD patterns always match the whole value, contract patterns anywhere unless anchored: ^ORD$ exports as ORD, ORD as .*(ORD).*.
  • A property with the custom property xmlNode: attribute becomes an attribute, without the @ prefix the import gives an attribute named like another property, and one with xmlNode: text the text of an element with attributes. A schema's xmlNamespace custom property becomes the target namespace; a property's xmlNamespace: "" makes the element unqualified (form="unqualified"). An element in yet another namespace would need a schema of its own, so it stays in the target namespace, with a warning. datacontract import xsd sets all three, so an imported XML Schema exports back to an equivalent one.
  • An object without properties accepts any content (xs:anyType). A map becomes repeated entry elements with a key and a value.
  • A string with physicalType: list exports as an xs:list, and one with a union of built-in XSD types in physicalType (integer|date) as an xs:union, the way the import writes them.
  • XSD allows one lower and one upper bound per type, so when a property has both minimum and exclusiveMinimum (or both maximums), the tighter one is exported.
  • Every schema becomes a global element of the same XML Schema, which has one target namespace: when schemas declare different xmlNamespaces, the first one is used, with a warning. --schema-name exports a single schema.
  • Names that are not valid XML names are changed to valid ones, with a warning.

XML Schema has nothing for primaryKey, unique, string formats, examples, and quality rules, so they are not exported. The digits of a decimal come from the precision and scale custom properties; a physicalType such as NUMERIC(12,2) is not read.

All options: datacontract export xsd.