Chapter 04: Schema composition¶
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
@dataclass
class OrderType:
"""
:ivar number:
"""
number: Optional[str] = field(
default=None,
metadata=dict(
name="number",
type="Element",
namespace="",
required=True
)
)
@dataclass
class Order(OrderType):
class Meta:
name = "order"
namespace = "http://datypic.com/ord"
Example 4-1.2 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
@dataclass
class OrderNumType:
"""
:ivar value:
"""
class Meta:
namespace = "http://datypic.com/ord"
value: Optional[str] = field(
default=None,
metadata=dict(
name="value",
type="Restriction"
)
)
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(
name="number",
type="Element",
namespace="",
required=True
)
)
customer: Optional[CustomerType] = field(
default=None,
metadata=dict(
name="customer",
type="Element",
namespace="",
required=True
)
)
@dataclass
class Order(OrderType):
class Meta:
name = "order"
Example 4-2.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(
name="name",
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,
)
@dataclass
class OrderType:
"""
:ivar number:
:ivar items:
"""
number: Optional[str] = field(
default=None,
metadata=dict(
name="number",
type="Element",
namespace="",
required=True
)
)
items: Optional[ItemsType] = field(
default=None,
metadata=dict(
name="items",
type="Element",
namespace="",
required=True
)
)
@dataclass
class Order(OrderType):
class Meta:
name = "order"
namespace = "http://datypic.com/ord"
Example 4-3.2 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,
)
@dataclass
class ItemsType:
"""
:ivar product:
"""
product: Optional[ProductType] = field(
default=None,
metadata=dict(
name="product",
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,
)
@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.2 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
@dataclass
class OrderSummary:
"""
:ivar value:
"""
class Meta:
name = "orderSummary"
namespace = "http://datypic.com/ord"
value: Optional[str] = field(
default=None,
metadata=dict(
name="value",
type="Extension"
)
)
Example 4-5.3 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
@dataclass
class OrderDetails:
"""
:ivar value:
"""
class Meta:
name = "orderDetails"
namespace = "http://datypic.com/ord"
value: Optional[str] = field(
default=None,
metadata=dict(
name="value",
type="Extension"
)
)
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,
)
@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-6.2 Proxy schema to avoid multiple imports¶
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/ord" targetNamespace="http://datypic.com/ord">
<xs:include namespace="http://datypic.com/ord" schemaLocation="example04052.xsd" />
<xs:include namespace="http://datypic.com/ord" schemaLocation="example04053.xsd" />
</xs:schema>
Warning
Illegal definition
Example 4-7 Illegal duplication of element names¶
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/ord" targetNamespace="http://datypic.com/ord">
<xs:include schemaLocation="ord2.xsd" />
<xs:element name="order" type="OrderType" />
</xs:schema>
Warning
Illegal definition
Example 4-7.2 Illegal duplication of element names¶
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/ord" targetNamespace="http://datypic.com/ord">
<xs:element name="order" type="OrderType" />
</xs:schema>
Warning
Illegal definition
Example 4-8 Illegal Missing component¶
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/ord" targetNamespace="http://datypic.com/ord">
<xs:element name="number" type="xs:integer" />
<xs:element name="order" type="OrderType" />
</xs:schema>
Warning
Illegal definition
Samples Source
Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR