Definitive XML Schema

Historically this was the first integration test suite of xsdata and is still being used to make sure things still work between minor code changes.

It’s based on the samples from the book Definitive XML Schema by Priscilla Walmsley that cover most of the basic XML Schema language traits

The test suite is using the auto generated models to parse the sample documents and serializes them back to JSON and XML documents.

Samples Source

Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR


#08 - Simple types

Code Generation

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:annotation>
    <xsd:documentation>
      This example illustrates various simple type restrictions using bounds facets,
      patterns, and enumerations. White space is used in the instance to illustrate
      that white space processing takes place before validation.
    </xsd:documentation>
  </xsd:annotation>

  <xsd:element name="sizes" type="SizesType" />

  <xsd:complexType name="SizesType">
    <xsd:choice maxOccurs="unbounded">
      <xsd:element name="dressSize" type="DressSizeType" />
      <xsd:element name="mediumDressSize" type="MediumDressSizeType" />
      <xsd:element name="smallDressSize" type="SmallDressSizeType" />
      <xsd:element name="smlxSize" type="SMLXSizeType" />
      <xsd:element name="xsmlxSize" type="XSMLXSizeType" />
    </xsd:choice>
  </xsd:complexType>

  <xsd:simpleType name="DressSizeType">
    <xsd:restriction base="xsd:integer">
      <xsd:minInclusive value="2" />
      <xsd:maxInclusive value="18" />
      <xsd:pattern value="\d{1,2}" />
    </xsd:restriction>
  </xsd:simpleType>

  <xsd:simpleType name="MediumDressSizeType">
    <xsd:restriction base="DressSizeType">
      <xsd:minInclusive value="8" />
      <xsd:maxInclusive value="12" />
    </xsd:restriction>
  </xsd:simpleType>

  <xsd:simpleType name="SmallDressSizeType">
    <xsd:restriction base="DressSizeType">
      <xsd:minInclusive value="2" />
      <xsd:maxInclusive value="6" />
      <xsd:pattern value="\d{1}" />
    </xsd:restriction>
  </xsd:simpleType>

  <xsd:simpleType name="SMLXSizeType">
    <xsd:restriction base="xsd:token">
      <xsd:enumeration value="small" />
      <xsd:enumeration value="medium" />
      <xsd:enumeration value="large" />
      <xsd:enumeration value="extra large" />
    </xsd:restriction>
  </xsd:simpleType>

  <xsd:simpleType name="XSMLXSizeType">
    <xsd:union memberTypes="SMLXSizeType">
      <xsd:simpleType>
        <xsd:restriction base="xsd:token">
          <xsd:enumeration value="extra small" />
        </xsd:restriction>
      </xsd:simpleType>
    </xsd:union>
  </xsd:simpleType>

</xsd:schema>
from dataclasses import dataclass, field
from enum import Enum
from typing import List


class SmlxsizeType(Enum):
    SMALL = "small"
    MEDIUM = "medium"
    LARGE = "large"
    EXTRA_LARGE = "extra large"


class XsmlxsizeType(Enum):
    SMALL = "small"
    MEDIUM = "medium"
    LARGE = "large"
    EXTRA_LARGE = "extra large"
    EXTRA_SMALL = "extra small"


@dataclass
class SizesType:
    dress_size: List[str] = field(
        default_factory=list,
        metadata={
            "name": "dressSize",
            "type": "Element",
            "namespace": "",
            "min_inclusive": "2",
            "max_inclusive": "18",
            "pattern": r"\d{1,2}",
        }
    )
    medium_dress_size: List[str] = field(
        default_factory=list,
        metadata={
            "name": "mediumDressSize",
            "type": "Element",
            "namespace": "",
            "min_inclusive": "8",
            "max_inclusive": "12",
            "pattern": r"\d{1,2}",
        }
    )
    small_dress_size: List[str] = field(
        default_factory=list,
        metadata={
            "name": "smallDressSize",
            "type": "Element",
            "namespace": "",
            "min_inclusive": "2",
            "max_inclusive": "6",
            "pattern": r"\d{1}",
        }
    )
    smlx_size: List[SmlxsizeType] = field(
        default_factory=list,
        metadata={
            "name": "smlxSize",
            "type": "Element",
            "namespace": "",
        }
    )
    xsmlx_size: List[XsmlxsizeType] = field(
        default_factory=list,
        metadata={
            "name": "xsmlxSize",
            "type": "Element",
            "namespace": "",
        }
    )


@dataclass
class Sizes(SizesType):
    class Meta:
        name = "sizes"

Data Binding

<sizes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="chapter09.xsd">
  <dressSize>06</dressSize>
  <mediumDressSize>12</mediumDressSize>
  <smallDressSize>6</smallDressSize>
  <smlxSize>extra
  large</smlxSize>
  <xsmlxSize>extra    small</xsmlxSize>
</sizes>
<sizes>
  <dressSize>06</dressSize>
  <mediumDressSize>12</mediumDressSize>
  <smallDressSize>6</smallDressSize>
  <smlxSize>extra large</smlxSize>
  <xsmlxSize>extra small</xsmlxSize>
</sizes>
{
    "dressSize": [
        "06"
    ],
    "mediumDressSize": [
        "12"
    ],
    "smallDressSize": [
        "6"
    ],
    "smlxSize": [
        "extra large"
    ],
    "xsmlxSize": [
        "extra small"
    ]
}

Samples Source

Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR

#12 - Complex types

Code Generation

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:annotation>
    <xsd:documentation>
      This example illustrates complex types that are not derived from other specified types.
      It includes an example of each content type: element-only, simple, empty and mixed.
    </xsd:documentation>
  </xsd:annotation>

  <xsd:element name="items" type="ItemsType" />

  <xsd:complexType name="ItemsType">
    <xsd:choice minOccurs="0" maxOccurs="unbounded">
      <xsd:element name="shirt" type="ProductType" />
      <xsd:element name="hat" type="ProductType" />
      <xsd:element name="umbrella" type="ProductType" />
    </xsd:choice>
  </xsd:complexType>

  <!-- Element-only content -->
  <xsd:complexType name="ProductType">
    <xsd:sequence>
      <xsd:element name="number" type="xsd:integer" />
      <xsd:element name="name" type="xsd:string" />
      <xsd:choice minOccurs="0" maxOccurs="unbounded">
        <xsd:element name="size" type="SizeType" />
        <xsd:element name="color" type="ColorType" />
        <xsd:element name="description" type="DescriptionType" />
      </xsd:choice>
    </xsd:sequence>
    <xsd:attribute name="effDate" type="xsd:date" default="1900-01-01" />
    <xsd:anyAttribute namespace="##other" processContents="lax" />
  </xsd:complexType>

  <!-- Simple content -->
  <xsd:complexType name="SizeType">
    <xsd:simpleContent>
      <xsd:extension base="xsd:integer">
        <xsd:attribute name="system" type="xsd:token" />
      </xsd:extension>
    </xsd:simpleContent>
  </xsd:complexType>

  <!-- Empty content -->
  <xsd:complexType name="ColorType">
    <xsd:attribute name="value" type="xsd:string" />
  </xsd:complexType>

  <!-- Mixed content -->
  <xsd:complexType name="DescriptionType" mixed="true">
    <xsd:sequence>
      <xsd:any namespace="http://www.w3.org/1999/xhtml" minOccurs="0" maxOccurs="unbounded" processContents="skip" />
    </xsd:sequence>
  </xsd:complexType>

</xsd:schema>
from dataclasses import dataclass, field
from typing import Dict, List, Optional
from xsdata.models.datatype import XmlDate


@dataclass
class ColorType:
    value: Optional[str] = field(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )


@dataclass
class DescriptionType:
    w3_org_1999_xhtml_element: List[object] = field(
        default_factory=list,
        metadata={
            "type": "Wildcard",
            "namespace": "http://www.w3.org/1999/xhtml",
            "mixed": True,
        }
    )


@dataclass
class SizeType:
    value: Optional[int] = field(
        default=None,
        metadata={
            "required": True,
        }
    )
    system: Optional[str] = field(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )


@dataclass
class ProductType:
    number: Optional[int] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    name: Optional[str] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    size_or_color_or_description: List[object] = field(
        default_factory=list,
        metadata={
            "type": "Elements",
            "choices": (
                {
                    "name": "size",
                    "type": SizeType,
                    "namespace": "",
                },
                {
                    "name": "color",
                    "type": ColorType,
                    "namespace": "",
                },
                {
                    "name": "description",
                    "type": DescriptionType,
                    "namespace": "",
                },
            ),
        }
    )
    eff_date: XmlDate = field(
        default=XmlDate(1900, 1, 1),
        metadata={
            "name": "effDate",
            "type": "Attribute",
        }
    )
    other_attributes: Dict[str, str] = field(
        default_factory=dict,
        metadata={
            "type": "Attributes",
            "namespace": "##other",
        }
    )


@dataclass
class ItemsType:
    shirt_or_hat_or_umbrella: List[object] = field(
        default_factory=list,
        metadata={
            "type": "Elements",
            "choices": (
                {
                    "name": "shirt",
                    "type": ProductType,
                    "namespace": "",
                },
                {
                    "name": "hat",
                    "type": ProductType,
                    "namespace": "",
                },
                {
                    "name": "umbrella",
                    "type": ProductType,
                    "namespace": "",
                },
            ),
        }
    )


@dataclass
class Items(ItemsType):
    class Meta:
        name = "items"

Data Binding

<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="chapter13.xsd"
       xmlns:xhtml="http://www.w3.org/1999/xhtml"
       xmlns:oth="http://example.org/oth">
  <shirt effDate="2002-04-02">
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <size system="US-DRESS">10</size>
    <color value="blue"/>
    <description>
      This shirt is the <xhtml:b>best-selling</xhtml:b> shirt in
      our catalog! <xhtml:br/> Note: runs large.
    </description>
  </shirt>
  <hat oth:custom="12">
    <number>557</number>
    <name>Ten-Gallon Hat</name>
    <color value="red"/>
  </hat>
</items>
<items>
  <shirt effDate="2002-04-02">
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <size system="US-DRESS">10</size>
    <color value="blue"/>
    <description>
      This shirt is the <xhtml:b xmlns:xhtml="http://www.w3.org/1999/xhtml">best-selling</xhtml:b> shirt in
      our catalog! <xhtml:br xmlns:xhtml="http://www.w3.org/1999/xhtml"/> Note: runs large.
    </description>
  </shirt>
  <hat xmlns:ns0="http://example.org/oth" effDate="1900-01-01" ns0:custom="12">
    <number>557</number>
    <name>Ten-Gallon Hat</name>
    <color value="red"/>
  </hat>
</items>
{
    "shirt_or_hat_or_umbrella": [
        {
            "number": 557,
            "name": "Short-Sleeved Linen Blouse",
            "size_or_color_or_description": [
                {
                    "value": 10,
                    "system": "US-DRESS"
                },
                {
                    "value": "blue"
                },
                {
                    "w3_org_1999_xhtml_element": [
                        "\n      This shirt is the ",
                        {
                            "qname": "{http://www.w3.org/1999/xhtml}b",
                            "text": "best-selling",
                            "tail": " shirt in\n      our catalog! ",
                            "children": [],
                            "attributes": {}
                        },
                        {
                            "qname": "{http://www.w3.org/1999/xhtml}br",
                            "text": "",
                            "tail": " Note: runs large.\n    ",
                            "children": [],
                            "attributes": {}
                        }
                    ]
                }
            ],
            "effDate": "2002-04-02",
            "other_attributes": {}
        },
        {
            "qname": "hat",
            "value": {
                "number": 557,
                "name": "Ten-Gallon Hat",
                "size_or_color_or_description": [
                    {
                        "value": "red"
                    }
                ],
                "effDate": "1900-01-01",
                "other_attributes": {
                    "{http://example.org/oth}custom": "12"
                }
            },
            "substituted": false
        }
    ]
}

Samples Source

Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR

#01 - Schemas: An introduction

Code Generation

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="product" type="ProductType" />
  <xsd:complexType name="ProductType">
    <xsd:sequence>
      <xsd:element name="number" type="xsd:integer" />
      <xsd:element name="size" type="SizeType" />
    </xsd:sequence>
    <xsd:attribute name="effDate" type="xsd:date" />
  </xsd:complexType>
  <xsd:simpleType name="SizeType">
    <xsd:restriction base="xsd:integer">
      <xsd:minInclusive value="2" />
      <xsd:maxInclusive value="18" />
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>
from dataclasses import dataclass, field
from typing import Optional
from xsdata.models.datatype import XmlDate


@dataclass
class ProductType:
    number: Optional[int] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    size: Optional[int] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
            "min_inclusive": 2,
            "max_inclusive": 18,
        }
    )
    eff_date: Optional[XmlDate] = field(
        default=None,
        metadata={
            "name": "effDate",
            "type": "Attribute",
        }
    )


@dataclass
class Product(ProductType):
    class Meta:
        name = "product"

Data Binding

<product effDate="2001-04-02"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="chapter01.xsd">
  <number>557</number>
  <size>10</size>
</product>
<product effDate="2001-04-02">
  <number>557</number>
  <size>10</size>
</product>
{
    "number": 557,
    "size": 10,
    "effDate": "2001-04-02"
}

Samples Source

Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR

#03 - Namespaces

Code Generation

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:ord="http://example.org/ord">

  <xsd:annotation>
    <xsd:documentation>
      This provides an example of validating an instance with multiple namespaces.
      chapter03env.xsd imports chapter03ord.xsd, which imports chapter03prod.xsd, which
      imports chapter03prod2.xsd. The "form" attribute is used to indicate whether
      element-type and/or attribute names should be qualified in the instance.
    </xsd:documentation>
  </xsd:annotation>

  <xsd:import namespace="http://example.org/ord" schemaLocation="chapter03ord.xsd" />

  <xsd:element name="envelope" type="EnvelopeType" />

  <xsd:complexType name="EnvelopeType">
    <xsd:sequence>
      <xsd:element ref="ord:order" maxOccurs="unbounded" />
    </xsd:sequence>
  </xsd:complexType>

</xsd:schema>
from dataclasses import dataclass, field
from typing import List
from tests.fixtures.defxmlschema.chapter03ord import Order


@dataclass
class EnvelopeType:
    order: List[Order] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "namespace": "http://example.org/ord",
            "min_occurs": 1,
        }
    )


@dataclass
class Envelope(EnvelopeType):
    class Meta:
        name = "envelope"

Data Binding

<envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="chapter03.xsd">
  <order  xmlns="http://example.org/ord"
          xmlns:prod="http://example.org/prod">
    <number>123ABBCC123</number>
    <items>
      <product xmlns="http://example.org/prod">
        <number prod:id="prod557">557</number>
        <name xmlns="">Short-Sleeved Linen Blouse</name>
        <prod:size system="US-DRESS">10</prod:size>
        <prod:color xmlns:prod="http://example.org/prod2"
                    prod:value="blue"/>
      </product>
    </items>
  </order>
</envelope>
<envelope>
  <ns0:order xmlns:ns0="http://example.org/ord">
    <ns0:number>123ABBCC123</ns0:number>
    <ns0:items>
      <ns1:product xmlns:ns1="http://example.org/prod">
        <ns1:number ns1:id="prod557">557</ns1:number>
        <name>Short-Sleeved Linen Blouse</name>
        <ns1:size system="US-DRESS">10</ns1:size>
        <ns2:color xmlns:ns2="http://example.org/prod2" ns2:value="blue"/>
      </ns1:product>
    </ns0:items>
  </ns0:order>
</envelope>
{
    "order": [
        {
            "number": "123ABBCC123",
            "items": {
                "product": [
                    {
                        "number": {
                            "value": 557,
                            "id": "prod557"
                        },
                        "name": "Short-Sleeved Linen Blouse",
                        "size": {
                            "value": 10,
                            "system": "US-DRESS"
                        },
                        "color": {
                            "value": "blue"
                        }
                    }
                ]
            }
        }
    ]
}

Samples Source

Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR

#16 - Substitution groups

Code Generation

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:annotation>
    <xsd:documentation>
      This example illustrates substitution groups.
    </xsd:documentation>
  </xsd:annotation>

  <xsd:element name="items" type="ItemsType" />
  <xsd:complexType name="ItemsType">
    <xsd:sequence>
      <xsd:element ref="product" maxOccurs="unbounded" />
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="product" type="ProductType" />
  <xsd:complexType name="ProductType">
    <xsd:sequence>
      <xsd:element name="number" type="xsd:integer" />
      <xsd:element name="name" type="xsd:string" />
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="shirt" type="ShirtType" substitutionGroup="product" />
  <xsd:complexType name="ShirtType">
    <xsd:complexContent>
      <xsd:extension base="ProductType">
        <xsd:sequence>
          <xsd:element name="size" type="ShirtSizeType" />
          <xsd:element name="color" type="ColorType" />
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:element name="hat" substitutionGroup="product">
    <xsd:complexType>
      <xsd:complexContent>
        <xsd:extension base="ProductType">
          <xsd:sequence>
            <xsd:element name="size" type="HatSizeType" />
          </xsd:sequence>
        </xsd:extension>
      </xsd:complexContent>
    </xsd:complexType>
  </xsd:element>

  <xsd:element name="umbrella" substitutionGroup="product" />

  <xsd:complexType name="ShirtSizeType">
    <xsd:simpleContent>
      <xsd:extension base="xsd:integer">
        <xsd:attribute name="system" type="xsd:token" />
      </xsd:extension>
    </xsd:simpleContent>
  </xsd:complexType>

  <xsd:complexType name="HatSizeType">
    <xsd:simpleContent>
      <xsd:extension base="xsd:token">
        <xsd:attribute name="system" type="xsd:token" />
      </xsd:extension>
    </xsd:simpleContent>
  </xsd:complexType>

  <xsd:complexType name="ColorType">
    <xsd:attribute name="value" type="xsd:string" />
  </xsd:complexType>

</xsd:schema>
from dataclasses import dataclass, field
from typing import List, Optional


@dataclass
class ColorType:
    value: Optional[str] = field(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )


@dataclass
class HatSizeType:
    value: Optional[str] = field(
        default=None,
        metadata={
            "required": True,
        }
    )
    system: Optional[str] = field(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )


@dataclass
class ProductType:
    number: Optional[int] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    name: Optional[str] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )


@dataclass
class ShirtSizeType:
    value: Optional[int] = field(
        default=None,
        metadata={
            "required": True,
        }
    )
    system: Optional[str] = field(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )


@dataclass
class Umbrella:
    class Meta:
        name = "umbrella"

    any_element: Optional[object] = field(
        default=None,
        metadata={
            "type": "Wildcard",
            "namespace": "##any",
            "required": True,
        }
    )


@dataclass
class ShirtType(ProductType):
    size: Optional[ShirtSizeType] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    color: Optional[ColorType] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )


@dataclass
class Hat(ProductType):
    class Meta:
        name = "hat"

    size: Optional[HatSizeType] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )


@dataclass
class Product(ProductType):
    class Meta:
        name = "product"


@dataclass
class Shirt(ShirtType):
    class Meta:
        name = "shirt"


@dataclass
class ItemsType:
    umbrella: List[Umbrella] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "min_occurs": 1,
        }
    )
    hat: List[Hat] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "min_occurs": 1,
        }
    )
    shirt: List[Shirt] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "min_occurs": 1,
        }
    )
    product: List[Product] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "min_occurs": 1,
        }
    )


@dataclass
class Items(ItemsType):
    class Meta:
        name = "items"

Data Binding

<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="chapter16.xsd">
  <product>
    <number>999</number>
    <name>Special Seasonal</name>
  </product>
  <shirt>
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <size>10</size>
    <color value="blue"/>
  </shirt>
  <hat>
    <number>563</number>
    <name>Ten-Gallon Hat</name>
    <size>L</size>
  </hat>
  <umbrella>
    <number>443</number>
    <name>Deluxe Golf Umbrella</name>
  </umbrella>
</items>
<items>
  <umbrella>
    <number>443</number>
    <name>Deluxe Golf Umbrella</name>
  </umbrella>
  <hat>
    <number>563</number>
    <name>Ten-Gallon Hat</name>
    <size>L</size>
  </hat>
  <shirt>
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <size>10</size>
    <color value="blue"/>
  </shirt>
  <product>
    <number>999</number>
    <name>Special Seasonal</name>
  </product>
</items>
{
    "umbrella": [
        {
            "any_element": {
                "qname": null,
                "text": null,
                "tail": null,
                "children": [
                    {
                        "qname": "number",
                        "text": "443",
                        "tail": null,
                        "children": [],
                        "attributes": {}
                    },
                    {
                        "qname": "name",
                        "text": "Deluxe Golf Umbrella",
                        "tail": null,
                        "children": [],
                        "attributes": {}
                    }
                ],
                "attributes": {}
            }
        }
    ],
    "hat": [
        {
            "number": 563,
            "name": "Ten-Gallon Hat",
            "size": {
                "value": "L",
                "system": null
            }
        }
    ],
    "shirt": [
        {
            "number": 557,
            "name": "Short-Sleeved Linen Blouse",
            "size": {
                "value": 10,
                "system": null
            },
            "color": {
                "value": "blue"
            }
        }
    ],
    "product": [
        {
            "number": 999,
            "name": "Special Seasonal"
        }
    ]
}

Samples Source

Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR

#04 - Schema composition

Code Generation

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://example.org/ord"
            xmlns="http://example.org/ord"
            xmlns:prod="http://example.org/prod">

  <xsd:annotation>
    <xsd:documentation>
      This pulls together examples 4-3, 4-4 and 4-5, illustrating include, chameleon include,
      and import, respectively. Note that only the root element of the instance is prefixed,
      because all elements in all schema documents are declared locally and no
      elementFormDefault is specified.
    </xsd:documentation>
  </xsd:annotation>

  <xsd:include schemaLocation="chapter04ord2.xsd" />
  <xsd:include schemaLocation="chapter04cust.xsd" />
  <xsd:import namespace="http://example.org/prod" schemaLocation="chapter04prod.xsd" />

  <xsd:element name="order" type="OrderType" />
  <xsd:complexType name="OrderType">
    <xsd:sequence>
      <xsd:element name="number" type="OrderNumType" />
      <xsd:element name="customer" type="CustomerType" />
      <xsd:element name="items" type="prod:ItemsType" />
    </xsd:sequence>
  </xsd:complexType>

</xsd:schema>
from dataclasses import dataclass, field
from typing import Optional
from tests.fixtures.defxmlschema.chapter04cust import CustomerType
from tests.fixtures.defxmlschema.chapter04prod import ItemsType

__NAMESPACE__ = "http://example.org/ord"


@dataclass
class OrderType:
    number: Optional[str] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    customer: Optional[CustomerType] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    items: Optional[ItemsType] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )


@dataclass
class Order(OrderType):
    class Meta:
        name = "order"
        namespace = "http://example.org/ord"

Data Binding

<ord:order  xmlns:ord="http://example.org/ord"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://example.org/ord chapter04.xsd">
  <number>123ABBCC123</number>
  <customer>
    <name>Priscilla Walmsley</name>
    <number>15466</number>
  </customer>
  <items>
    <product>
      <number>557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <size system="US-DRESS">10</size>
      <color value="blue"/>
    </product>
  </items>
</ord:order>
<ns0:order xmlns:ns0="http://example.org/ord">
  <number>123ABBCC123</number>
  <customer>
    <name>Priscilla Walmsley</name>
    <number>15466</number>
  </customer>
  <items>
    <product>
      <number>557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <size system="US-DRESS">10</size>
      <color value="blue"/>
    </product>
  </items>
</ns0:order>
{
    "number": "123ABBCC123",
    "customer": {
        "name": "Priscilla Walmsley",
        "number": 15466
    },
    "items": {
        "product": {
            "number": 557,
            "name": "Short-Sleeved Linen Blouse",
            "size": {
                "value": 10,
                "system": "US-DRESS"
            },
            "color": {
                "value": "blue"
            }
        }
    }
}

Samples Source

Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR

#17 - Identity constraints

Code Generation

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:annotation>
    <xsd:documentation>
      This example illustrates identity constraints. Each child of items must have a number attribute whose value is unique within order. All product descendents of order must have a number child whose value matches one of these unique product numbers.
    </xsd:documentation>
  </xsd:annotation>

  <xsd:element name="order" type="OrderType">
    <xsd:keyref name="prodNumKeyRef" refer="prodNumKey">
      <xsd:selector xpath="items/*" />
      <xsd:field xpath="@number" />
    </xsd:keyref>
    <xsd:key name="prodNumKey">
      <xsd:selector xpath=".//product" />
      <xsd:field xpath="number" />
    </xsd:key>
  </xsd:element>

  <xsd:complexType name="OrderType">
    <xsd:sequence>
      <xsd:element name="number" type="xsd:string" />
      <xsd:element name="items" type="ItemsType" />
      <xsd:element name="products" type="ProductsType" />
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="ItemsType">
    <xsd:choice maxOccurs="unbounded">
      <xsd:element name="shirt" type="ProductOrderType" />
      <xsd:element name="hat" type="ProductOrderType" />
    </xsd:choice>
  </xsd:complexType>

  <xsd:complexType name="ProductOrderType">
    <xsd:sequence>
      <xsd:element name="quantity" type="xsd:integer" />
      <xsd:element name="color" type="ColorType" minOccurs="0" />
    </xsd:sequence>
    <xsd:attribute name="number" type="xsd:integer" />
  </xsd:complexType>

  <xsd:complexType name="ProductsType">
    <xsd:sequence>
      <xsd:element name="product" type="ProductType" maxOccurs="unbounded" />
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="ProductType">
    <xsd:sequence>
      <xsd:element name="number" type="xsd:integer" />
      <xsd:element name="name" type="xsd:string" />
      <xsd:element name="price" type="PriceType" />
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="ColorType">
    <xsd:attribute name="value" type="xsd:string" />
  </xsd:complexType>

  <xsd:complexType name="PriceType">
    <xsd:simpleContent>
      <xsd:extension base="xsd:decimal">
        <xsd:attribute name="currency" type="xsd:token" />
      </xsd:extension>
    </xsd:simpleContent>
  </xsd:complexType>

</xsd:schema>
from dataclasses import dataclass, field
from decimal import Decimal
from typing import List, Optional


@dataclass
class ColorType:
    value: Optional[str] = field(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )


@dataclass
class PriceType:
    value: Optional[Decimal] = field(
        default=None,
        metadata={
            "required": True,
        }
    )
    currency: Optional[str] = field(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )


@dataclass
class ProductOrderType:
    quantity: Optional[int] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    color: Optional[ColorType] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
        }
    )
    number: Optional[int] = field(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )


@dataclass
class ProductType:
    number: Optional[int] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    name: Optional[str] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    price: Optional[PriceType] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )


@dataclass
class ItemsType:
    shirt_or_hat: List[object] = field(
        default_factory=list,
        metadata={
            "type": "Elements",
            "choices": (
                {
                    "name": "shirt",
                    "type": ProductOrderType,
                    "namespace": "",
                },
                {
                    "name": "hat",
                    "type": ProductOrderType,
                    "namespace": "",
                },
            ),
        }
    )


@dataclass
class ProductsType:
    product: List[ProductType] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "namespace": "",
            "min_occurs": 1,
        }
    )


@dataclass
class OrderType:
    number: Optional[str] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    items: Optional[ItemsType] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    products: Optional[ProductsType] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )


@dataclass
class Order(OrderType):
    class Meta:
        name = "order"

Data Binding

<order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="chapter17.xsd">
  <number>123ABBCC123</number>
  <items>
    <shirt number="557">
      <quantity>1</quantity>
      <color value="blue"/>
    </shirt>
    <shirt number="557">
      <quantity>1</quantity>
      <color value="sage"/>
    </shirt>
    <hat number="563">
      <quantity>1</quantity>
    </hat>
  </items>
  <products>
    <product>
      <number>557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <price currency="USD">29.99</price>
    </product>
    <product>
      <number>563</number>
      <name>Ten-Gallon Hat</name>
      <price currency="USD">69.99</price>
    </product>
  </products>
</order>
<order>
  <number>123ABBCC123</number>
  <items>
    <shirt number="557">
      <quantity>1</quantity>
      <color value="blue"/>
    </shirt>
    <shirt number="557">
      <quantity>1</quantity>
      <color value="sage"/>
    </shirt>
    <hat number="563">
      <quantity>1</quantity>
    </hat>
  </items>
  <products>
    <product>
      <number>557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <price currency="USD">29.99</price>
    </product>
    <product>
      <number>563</number>
      <name>Ten-Gallon Hat</name>
      <price currency="USD">69.99</price>
    </product>
  </products>
</order>
{
    "number": "123ABBCC123",
    "items": {
        "shirt_or_hat": [
            {
                "quantity": 1,
                "color": {
                    "value": "blue"
                },
                "number": 557
            },
            {
                "quantity": 1,
                "color": {
                    "value": "sage"
                },
                "number": 557
            },
            {
                "qname": "hat",
                "value": {
                    "quantity": 1,
                    "color": null,
                    "number": 563
                },
                "substituted": false
            }
        ]
    },
    "products": {
        "product": [
            {
                "number": 557,
                "name": "Short-Sleeved Linen Blouse",
                "price": {
                    "value": "29.99",
                    "currency": "USD"
                }
            },
            {
                "number": 563,
                "name": "Ten-Gallon Hat",
                "price": {
                    "value": "69.99",
                    "currency": "USD"
                }
            }
        ]
    }
}

Samples Source

Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR

#15 - Named groups

Code Generation

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:annotation>
    <xsd:documentation>
      This example illustrates named model groups and attribute groups.
    </xsd:documentation>
  </xsd:annotation>

  <xsd:element name="shirt" type="ShirtType" />

  <xsd:complexType name="ShirtType">
    <xsd:sequence>
      <xsd:group ref="ProductPropertyGroup" minOccurs="0" />
      <xsd:element name="size" type="SizeType" />
    </xsd:sequence>
    <xsd:attributeGroup ref="IdentifierGroup" />
    <xsd:attribute name="effDate" type="xsd:date" />
  </xsd:complexType>

  <xsd:group name="DescriptionGroup">
    <xsd:sequence>
      <xsd:element name="description" type="xsd:string" />
      <xsd:element name="comment" type="xsd:string" minOccurs="0" />
    </xsd:sequence>
  </xsd:group>

  <xsd:group name="ProductPropertyGroup">
    <xsd:sequence>
      <xsd:group ref="DescriptionGroup" />
      <xsd:element name="number" type="xsd:integer" />
      <xsd:element name="name" type="xsd:string" />
    </xsd:sequence>
  </xsd:group>

  <xsd:attributeGroup name="IdentifierGroup">
    <xsd:attribute name="id" type="xsd:ID" use="required" />
    <xsd:attribute name="version" type="xsd:decimal" />
  </xsd:attributeGroup>

  <xsd:complexType name="SizeType">
    <xsd:simpleContent>
      <xsd:extension base="xsd:integer">
        <xsd:attribute name="system" type="xsd:token" />
      </xsd:extension>
    </xsd:simpleContent>
  </xsd:complexType>

</xsd:schema>
from dataclasses import dataclass, field
from decimal import Decimal
from typing import Optional
from xsdata.models.datatype import XmlDate


@dataclass
class SizeType:
    value: Optional[int] = field(
        default=None,
        metadata={
            "required": True,
        }
    )
    system: Optional[str] = field(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )


@dataclass
class ShirtType:
    description: Optional[str] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
        }
    )
    comment: Optional[str] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
        }
    )
    number: Optional[int] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
        }
    )
    name: Optional[str] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
        }
    )
    size: Optional[SizeType] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    id: Optional[str] = field(
        default=None,
        metadata={
            "type": "Attribute",
            "required": True,
        }
    )
    version: Optional[Decimal] = field(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )
    eff_date: Optional[XmlDate] = field(
        default=None,
        metadata={
            "name": "effDate",
            "type": "Attribute",
        }
    )


@dataclass
class Shirt(ShirtType):
    class Meta:
        name = "shirt"

Data Binding

<shirt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="chapter15.xsd"
       id="P557" version="100">
  <description>This is a great shirt.</description>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <size system="US-DRESS">6</size>
</shirt>
<shirt id="P557" version="100">
  <description>This is a great shirt.</description>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <size system="US-DRESS">6</size>
</shirt>
{
    "description": "This is a great shirt.",
    "comment": null,
    "number": 557,
    "name": "Short-Sleeved Linen Blouse",
    "size": {
        "value": 6,
        "system": "US-DRESS"
    },
    "id": "P557",
    "version": "100",
    "effDate": null
}

Samples Source

Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR

#13 - Deriving complex types

Code Generation

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:annotation>
    <xsd:documentation>
      This example illustrates complex types that are derived from other specified types.
    </xsd:documentation>
  </xsd:annotation>

  <xsd:element name="items" type="ItemsType" />

  <xsd:complexType name="ItemsType">
    <xsd:choice minOccurs="0" maxOccurs="unbounded">
      <xsd:element name="hat" type="ProductType" />
      <xsd:element name="umbrella" type="RestrictedProductType" />
      <xsd:element name="shirt" type="ShirtType" />
    </xsd:choice>
  </xsd:complexType>

  <!--Empty Content Type-->
  <xsd:complexType name="ItemType" abstract="true">
    <xsd:attribute name="routingNum" type="xsd:integer" />
  </xsd:complexType>

  <!--Empty Content Extension (with Attribute Extension)-->
  <xsd:complexType name="ProductType">
    <xsd:complexContent>
      <xsd:extension base="ItemType">
        <xsd:sequence>
          <xsd:element name="number" type="xsd:integer" />
          <xsd:element name="name" type="xsd:string" />
          <xsd:element name="description" type="xsd:string" minOccurs="0" />
        </xsd:sequence>
        <xsd:attribute name="effDate" type="xsd:date" />
        <xsd:attribute name="lang" type="xsd:language" />
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <!--Complex Content Restriction-->
  <xsd:complexType name="RestrictedProductType">
    <xsd:complexContent>
      <xsd:restriction base="ProductType">
        <xsd:sequence>
          <xsd:element name="number" type="xsd:integer" />
          <xsd:element name="name" type="xsd:token" />
        </xsd:sequence>
        <xsd:attribute name="routingNum" type="xsd:short" use="required" />
        <xsd:attribute name="effDate" type="xsd:date" default="1900-01-01" />
        <xsd:attribute name="lang" use="prohibited" />
      </xsd:restriction>
    </xsd:complexContent>
  </xsd:complexType>

  <!--Complex Content Extension-->
  <xsd:complexType name="ShirtType">
    <xsd:complexContent>
      <xsd:extension base="RestrictedProductType">
        <xsd:choice maxOccurs="unbounded">
          <xsd:element name="size" type="SmallSizeType" />
          <xsd:element name="color" type="ColorType" />
        </xsd:choice>
        <xsd:attribute name="sleeve" type="xsd:integer" />
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <!--Simple Content Extension-->
  <xsd:complexType name="SizeType">
    <xsd:simpleContent>
      <xsd:extension base="xsd:integer">
        <xsd:attribute name="system" type="xsd:token" />
      </xsd:extension>
    </xsd:simpleContent>
  </xsd:complexType>

  <!--Simple Content Restriction-->
  <xsd:complexType name="SmallSizeType">
    <xsd:simpleContent>
      <xsd:restriction base="SizeType">
        <xsd:minInclusive value="2" />
        <xsd:maxInclusive value="6" />
        <xsd:attribute name="system" type="xsd:token" use="required" />
      </xsd:restriction>
    </xsd:simpleContent>
  </xsd:complexType>

  <xsd:complexType name="ColorType">
    <xsd:attribute name="value" type="xsd:string" />
  </xsd:complexType>

</xsd:schema>
from dataclasses import dataclass, field
from typing import List, Optional
from xsdata.models.datatype import XmlDate


@dataclass
class ColorType:
    value: Optional[str] = field(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )


@dataclass
class ItemType:
    routing_num: Optional[int] = field(
        default=None,
        metadata={
            "name": "routingNum",
            "type": "Attribute",
        }
    )


@dataclass
class SizeType:
    value: Optional[int] = field(
        default=None,
        metadata={
            "required": True,
        }
    )
    system: Optional[str] = field(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )


@dataclass
class ProductType(ItemType):
    number: Optional[int] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    name: Optional[str] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )
    description: Optional[str] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
        }
    )
    eff_date: Optional[XmlDate] = field(
        default=None,
        metadata={
            "name": "effDate",
            "type": "Attribute",
        }
    )
    lang: Optional[str] = field(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )


@dataclass
class SmallSizeType(SizeType):
    system: Optional[str] = field(
        default=None,
        metadata={
            "type": "Attribute",
            "required": True,
        }
    )


@dataclass
class RestrictedProductType(ProductType):
    routing_num: Optional[int] = field(
        default=None,
        metadata={
            "name": "routingNum",
            "type": "Attribute",
            "required": True,
        }
    )
    eff_date: XmlDate = field(
        default=XmlDate(1900, 1, 1),
        metadata={
            "name": "effDate",
            "type": "Attribute",
        }
    )


@dataclass
class ShirtType(RestrictedProductType):
    size: List[SmallSizeType] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "namespace": "",
        }
    )
    color: List[ColorType] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "namespace": "",
        }
    )
    sleeve: Optional[int] = field(
        default=None,
        metadata={
            "type": "Attribute",
        }
    )


@dataclass
class ItemsType:
    hat: List[ProductType] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "namespace": "",
        }
    )
    umbrella: List[RestrictedProductType] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "namespace": "",
        }
    )
    shirt: List[ShirtType] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "namespace": "",
        }
    )


@dataclass
class Items(ItemsType):
    class Meta:
        name = "items"

Data Binding

<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="chapter14.xsd">
  <hat routingNum="123456" effDate="2002-04-02" lang="en-US">
    <number>557</number>
    <name>Ten-Gallon Hat</name>
    <description>This is a great hat!</description>
  </hat>
  <umbrella routingNum="123" effDate="2002-04-02">
    <number>557</number>
    <name>Ten-Gallon Hat</name>
  </umbrella>
  <shirt routingNum="124" effDate="2002-04-02" sleeve="17">
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <color value="blue"/>
    <size system="US-DRESS">6</size>
  </shirt>
</items>
<items>
  <hat routingNum="123456" effDate="2002-04-02" lang="en-US">
    <number>557</number>
    <name>Ten-Gallon Hat</name>
    <description>This is a great hat!</description>
  </hat>
  <umbrella routingNum="123" effDate="2002-04-02">
    <number>557</number>
    <name>Ten-Gallon Hat</name>
  </umbrella>
  <shirt routingNum="124" effDate="2002-04-02" sleeve="17">
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <size system="US-DRESS">6</size>
    <color value="blue"/>
  </shirt>
</items>
{
    "hat": [
        {
            "routingNum": 123456,
            "number": 557,
            "name": "Ten-Gallon Hat",
            "description": "This is a great hat!",
            "effDate": "2002-04-02",
            "lang": "en-US"
        }
    ],
    "umbrella": [
        {
            "routingNum": 123,
            "number": 557,
            "name": "Ten-Gallon Hat",
            "description": null,
            "effDate": "2002-04-02",
            "lang": null
        }
    ],
    "shirt": [
        {
            "routingNum": 124,
            "number": 557,
            "name": "Short-Sleeved Linen Blouse",
            "description": null,
            "effDate": "2002-04-02",
            "lang": null,
            "size": [
                {
                    "value": 6,
                    "system": "US-DRESS"
                }
            ],
            "color": [
                {
                    "value": "blue"
                }
            ],
            "sleeve": 17
        }
    ]
}

Samples Source

Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR

#05 - Instances and schemas

Code Generation

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://example.org/ord"
            xmlns="http://example.org/ord"
            xmlns:prod="http://example.org/prod">

  <xsd:annotation>
    <xsd:documentation>
      This example shows how xsi:schemaLocation can be used to pull together multiple schema documents.
      An import is used to show the dependence on the prod namespace, but no schema location
      is provided in the schema; this type of "dangling" reference to product is allowed.
      This example also exhibits xsi:nil and xsi:type.
    </xsd:documentation>
  </xsd:annotation>

  <xsd:import namespace="http://example.org/prod" schemaLocation="chapter05prod.xsd" />

  <xsd:element name="order" type="OrderType" />
  <xsd:complexType name="OrderType">
    <xsd:sequence>
      <xsd:element name="items" type="ItemsType" />
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="ItemsType">
    <xsd:sequence>
      <xsd:element ref="prod:product" maxOccurs="unbounded" />
    </xsd:sequence>
  </xsd:complexType>

</xsd:schema>
from dataclasses import dataclass, field
from typing import List, Optional
from tests.fixtures.defxmlschema.chapter05prod import Product

__NAMESPACE__ = "http://example.org/ord"


@dataclass
class ItemsType:
    product: List[Product] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "namespace": "http://example.org/prod",
            "min_occurs": 1,
        }
    )


@dataclass
class OrderType:
    items: Optional[ItemsType] = field(
        default=None,
        metadata={
            "type": "Element",
            "namespace": "",
            "required": True,
        }
    )


@dataclass
class Order(OrderType):
    class Meta:
        name = "order"
        namespace = "http://example.org/ord"

Data Binding

<ord:order xmlns:ord="http://example.org/ord"
           xmlns:prod="http://example.org/prod"
           xmlns:xsd="http://www.w3.org/2001/XMLSchema"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://example.org/prod chapter05prod.xsd
                               http://example.org/ord chapter05.xsd">
  <items>
    <prod:product>
      <number xsi:type="xsd:short">557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <size xsi:nil="true"></size>
    </prod:product>
  </items>
</ord:order>
<ns0:order xmlns:ns0="http://example.org/ord">
  <items>
    <ns1:product xmlns:ns1="http://example.org/prod">
      <number>557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <size xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    </ns1:product>
  </items>
</ns0:order>
{
    "items": {
        "product": [
            {
                "number": 557,
                "name": "Short-Sleeved Linen Blouse",
                "size": {
                    "value": null,
                    "system": null
                }
            }
        ]
    }
}

Samples Source

Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR

#10 - Union and list types

Code Generation

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <xsd:annotation>
    <xsd:documentation>
      This example illustrates list and union types.
    </xsd:documentation>
  </xsd:annotation>

  <xsd:element name="sizes" type="SizesType" />

  <xsd:complexType name="SizesType">
    <xsd:choice maxOccurs="unbounded">
      <xsd:element name="size" type="SizeType" />
      <xsd:element name="smallSize" type="SmallSizeType" />
      <xsd:element name="internationalSize" type="InternationalSizeType" />
      <xsd:element name="availableSizes" type="AvailableSizesType" />
      <xsd:element name="applicableSizes" type="ApplicableSizesType" />
    </xsd:choice>
  </xsd:complexType>

  <xsd:simpleType name="SizeType">
    <xsd:union memberTypes="DressSizeType">
      <xsd:simpleType>
        <xsd:restriction base="xsd:token">
          <xsd:enumeration value="small" />
          <xsd:enumeration value="medium" />
          <xsd:enumeration value="large" />
        </xsd:restriction>
      </xsd:simpleType>
    </xsd:union>
  </xsd:simpleType>

  <xsd:simpleType name="DressSizeType">
    <xsd:restriction base="xsd:integer">
      <xsd:minInclusive value="2" />
      <xsd:maxInclusive value="18" />
    </xsd:restriction>
  </xsd:simpleType>

  <xsd:simpleType name="SmallSizeType">
    <xsd:restriction base="SizeType">
      <xsd:enumeration value="2" />
      <xsd:enumeration value="4" />
      <xsd:enumeration value="6" />
      <xsd:enumeration value="small" />
    </xsd:restriction>
  </xsd:simpleType>

  <xsd:simpleType name="InternationalSizeType">
    <xsd:union memberTypes="SizeType">
      <xsd:simpleType>
        <xsd:restriction base="xsd:integer">
          <xsd:minInclusive value="24" />
          <xsd:maxInclusive value="54" />
        </xsd:restriction>
      </xsd:simpleType>
    </xsd:union>
  </xsd:simpleType>

  <xsd:simpleType name="AvailableSizesType">
    <xsd:list itemType="SizeType" />
  </xsd:simpleType>

  <xsd:simpleType name="ApplicableSizesType">
    <xsd:restriction>
      <xsd:simpleType>
        <xsd:list itemType="SizeType" />
      </xsd:simpleType>
      <xsd:enumeration value="small medium large" />
      <xsd:enumeration value="2 4 6 8 10 12 14 16 18" />
    </xsd:restriction>
  </xsd:simpleType>

</xsd:schema>
from dataclasses import dataclass, field
from enum import Enum
from typing import List, Union


class ApplicableSizesType(Enum):
    SMALL_MEDIUM_LARGE = "small medium large"
    VALUE_2_4_6_8_10_12_14_16_18 = "2 4 6 8 10 12 14 16 18"


class SizeTypeValue(Enum):
    SMALL = "small"
    MEDIUM = "medium"
    LARGE = "large"


class SmallSizeType(Enum):
    VALUE_2 = 2
    VALUE_4 = 4
    VALUE_6 = 6
    SMALL = "small"


@dataclass
class SizesType:
    size: List[Union[int, SizeTypeValue]] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "namespace": "",
            "min_inclusive": 2,
            "max_inclusive": 18,
        }
    )
    small_size: List[SmallSizeType] = field(
        default_factory=list,
        metadata={
            "name": "smallSize",
            "type": "Element",
            "namespace": "",
        }
    )
    international_size: List[Union[int, SizeTypeValue]] = field(
        default_factory=list,
        metadata={
            "name": "internationalSize",
            "type": "Element",
            "namespace": "",
            "min_inclusive": 24,
            "max_inclusive": 54,
        }
    )
    available_sizes: List[List[Union[int, SizeTypeValue]]] = field(
        default_factory=list,
        metadata={
            "name": "availableSizes",
            "type": "Element",
            "namespace": "",
            "min_inclusive": 2,
            "max_inclusive": 18,
            "tokens": True,
        }
    )
    applicable_sizes: List[ApplicableSizesType] = field(
        default_factory=list,
        metadata={
            "name": "applicableSizes",
            "type": "Element",
            "namespace": "",
        }
    )


@dataclass
class Sizes(SizesType):
    class Meta:
        name = "sizes"

Data Binding

<sizes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="chapter11.xsd">
  <size>12</size>
  <size>medium</size>
  <smallSize>6</smallSize>
  <smallSize>small</smallSize>
  <internationalSize>12</internationalSize>
  <internationalSize>24</internationalSize>
  <internationalSize>large</internationalSize>
  <availableSizes>10 large 2</availableSizes>
  <applicableSizes>small medium large</applicableSizes>
</sizes>
<sizes>
  <size>12</size>
  <size>medium</size>
  <smallSize>6</smallSize>
  <smallSize>small</smallSize>
  <internationalSize>12</internationalSize>
  <internationalSize>24</internationalSize>
  <internationalSize>large</internationalSize>
  <availableSizes>10 large 2</availableSizes>
  <applicableSizes>small medium large</applicableSizes>
</sizes>
{
    "size": [
        12,
        "medium"
    ],
    "smallSize": [
        6,
        "small"
    ],
    "internationalSize": [
        12,
        24,
        "large"
    ],
    "availableSizes": [
        [
            10,
            "large",
            2
        ]
    ],
    "applicableSizes": [
        "small medium large"
    ]
}

Samples Source

Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR