#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,
    )
    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 = 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": null,
                            "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"
                }
            }
        }
    ]
}

Samples Source

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