Chapter 04

Schema composition

Binding Test

Schema

<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>

XML Document

<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>

xsData XML Document

<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>

xsData JSON

{
    "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"
            }
        }
    }
}

Example 4-1 Include

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/ord" targetNamespace="http://datypic.com/ord">
  <xs:include schemaLocation="example04012.xsd" />
  <xs:element name="order" type="OrderType" />
  <xs:complexType name="OrderType">
    <xs:sequence>
      <xs:element name="number" type="OrderNumType" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional

__NAMESPACE__ = "http://datypic.com/ord"


@dataclass
class OrderType:
    """
    :ivar number:
    """
    number: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace="",
            required=True
        )
    )


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

Example 4-1 Include

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/ord" targetNamespace="http://datypic.com/ord">
  <xs:simpleType name="OrderNumType">
    <xs:restriction base="xs:string" />
  </xs:simpleType>
  <xs:element name="OrderNumType" type="OrderNumType" />
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional

__NAMESPACE__ = "http://datypic.com/ord"


@dataclass
class OrderNumType:
    """
    :ivar value:
    """
    class Meta:
        namespace = "http://datypic.com/ord"

    value: Optional[str] = field(
        default=None,
        metadata=dict(
            required=True
        )
    )

Example 4-2 Chameleon include

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:include schemaLocation="example04022.xsd" />
  <xs:element name="order" type="OrderType" />
  <xs:complexType name="OrderType">
    <xs:sequence>
      <xs:element name="number" type="xs:string" />
      <xs:element name="customer" type="CustomerType" />
      <!-- ... -->
    </xs:sequence>
  </xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional
from tests.fixtures.defxmlschema.chapter04.example04022 import (
    CustomerType,
)


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


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

Example 4-2 Chameleon include

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="CustomerType">
    <xs:sequence>
      <xs:element name="name" type="CustNameType" />
      <!-- ... -->
    </xs:sequence>
  </xs:complexType>
  <xs:simpleType name="CustNameType">
    <xs:restriction base="xs:string" />
  </xs:simpleType>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional


@dataclass
class CustomerType:
    """
    :ivar name:
    """
    name: Optional[str] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace="",
            required=True
        )
    )

Example 4-3 Import

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/ord" xmlns:prod="http://datypic.com/prod" targetNamespace="http://datypic.com/ord">
  <xs:import namespace="http://datypic.com/prod" schemaLocation="example04032.xsd" />
  <xs:element name="order" type="OrderType" />
  <xs:complexType name="OrderType">
    <xs:sequence>
      <xs:element name="number" type="xs:string" />
      <xs:element name="items" type="prod:ItemsType" />
      <!-- ... -->
    </xs:sequence>
  </xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional
from tests.fixtures.defxmlschema.chapter04.example04032 import (
    ItemsType,
)

__NAMESPACE__ = "http://datypic.com/ord"


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


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

Example 4-3 Import

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/prod" targetNamespace="http://datypic.com/prod">
  <xs:include schemaLocation="../chapter02/example0210.xsd" />
  <xs:complexType name="ItemsType">
    <xs:sequence>
      <xs:element name="product" type="ProductType" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional
from tests.fixtures.defxmlschema.chapter02.example0210 import (
    ProductType,
)

__NAMESPACE__ = "http://datypic.com/prod"


@dataclass
class ItemsType:
    """
    :ivar product:
    """
    product: Optional[ProductType] = field(
        default=None,
        metadata=dict(
            type="Element",
            namespace="",
            required=True
        )
    )

Example 4-5 Multiple imports of the same namespace

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/root" xmlns:ord="http://datypic.com/ord" targetNamespace="http://datypic.com/root">
  <xs:import namespace="http://datypic.com/ord" schemaLocation="example04052.xsd" />
  <xs:import namespace="http://datypic.com/ord" schemaLocation="example04053.xsd" />
  <xs:element name="root" type="RootType" />
  <xs:complexType name="RootType">
    <xs:sequence>
      <xs:element ref="ord:orderSummary" />
      <xs:element ref="ord:orderDetails" />
      <!-- ... -->
    </xs:sequence>
  </xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional
from tests.fixtures.defxmlschema.chapter04.example04053 import (
    OrderDetails,
)
from tests.fixtures.defxmlschema.chapter04.example04052 import (
    OrderSummary,
)

__NAMESPACE__ = "http://datypic.com/root"


@dataclass
class RootType:
    """
    :ivar order_summary:
    :ivar order_details:
    """
    order_summary: Optional[OrderSummary] = field(
        default=None,
        metadata=dict(
            name="orderSummary",
            type="Element",
            namespace="http://datypic.com/ord",
            required=True
        )
    )
    order_details: Optional[OrderDetails] = field(
        default=None,
        metadata=dict(
            name="orderDetails",
            type="Element",
            namespace="http://datypic.com/ord",
            required=True
        )
    )


@dataclass
class Root(RootType):
    class Meta:
        name = "root"
        namespace = "http://datypic.com/root"

Example 4-5 Multiple imports of the same namespace

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/ord" targetNamespace="http://datypic.com/ord">
  <xs:element name="orderSummary" />
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional

__NAMESPACE__ = "http://datypic.com/ord"


@dataclass
class OrderSummary:
    """
    :ivar any_element:
    """
    class Meta:
        name = "orderSummary"
        namespace = "http://datypic.com/ord"

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

Example 4-5 Multiple imports of the same namespace

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/ord" targetNamespace="http://datypic.com/ord">
  <xs:element name="orderDetails" />
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional

__NAMESPACE__ = "http://datypic.com/ord"


@dataclass
class OrderDetails:
    """
    :ivar any_element:
    """
    class Meta:
        name = "orderDetails"
        namespace = "http://datypic.com/ord"

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

Example 4-6 Proxy schema to avoid multiple imports

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/root" xmlns:ord="http://datypic.com/ord" targetNamespace="http://datypic.com/root">
  <xs:import namespace="http://datypic.com/ord" schemaLocation="example04062.xsd" />
  <xs:element name="root" type="RootType" />
  <xs:complexType name="RootType">
    <xs:sequence>
      <xs:element ref="ord:orderSummary" />
      <xs:element ref="ord:orderDetails" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>
from dataclasses import dataclass, field
from typing import Optional
from tests.fixtures.defxmlschema.chapter04.example04053 import (
    OrderDetails,
)
from tests.fixtures.defxmlschema.chapter04.example04052 import (
    OrderSummary,
)

__NAMESPACE__ = "http://datypic.com/root"


@dataclass
class RootType:
    """
    :ivar order_summary:
    :ivar order_details:
    """
    order_summary: Optional[OrderSummary] = field(
        default=None,
        metadata=dict(
            name="orderSummary",
            type="Element",
            namespace="http://datypic.com/ord",
            required=True
        )
    )
    order_details: Optional[OrderDetails] = field(
        default=None,
        metadata=dict(
            name="orderDetails",
            type="Element",
            namespace="http://datypic.com/ord",
            required=True
        )
    )


@dataclass
class Root(RootType):
    class Meta:
        name = "root"
        namespace = "http://datypic.com/root"

Samples Source

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