#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