Chapter 12¶
Complex types¶
Binding Test¶
Schema
<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 lxml.etree import QName
from typing import Dict, List, Optional
@dataclass
class ColorType:
"""
:ivar value:
"""
value: Optional[str] = field(
default=None,
metadata=dict(
type="Attribute"
)
)
@dataclass
class DescriptionType:
"""
:ivar www_w3_org_1999_xhtml_element:
"""
www_w3_org_1999_xhtml_element: List[object] = field(
default_factory=list,
metadata=dict(
type="Wildcard",
namespace="http://www.w3.org/1999/xhtml",
min_occurs=0,
max_occurs=9223372036854775807
)
)
@dataclass
class SizeType:
"""
:ivar value:
:ivar system:
"""
value: Optional[int] = field(
default=None,
)
system: Optional[str] = field(
default=None,
metadata=dict(
type="Attribute"
)
)
@dataclass
class ProductType:
"""
:ivar number:
:ivar name:
:ivar size:
:ivar color:
:ivar description:
:ivar eff_date:
:ivar other_attributes:
"""
number: Optional[int] = field(
default=None,
metadata=dict(
type="Element",
namespace="",
required=True
)
)
name: Optional[str] = field(
default=None,
metadata=dict(
type="Element",
namespace="",
required=True
)
)
size: List[SizeType] = field(
default_factory=list,
metadata=dict(
type="Element",
namespace="",
min_occurs=0,
max_occurs=9223372036854775807
)
)
color: List[ColorType] = field(
default_factory=list,
metadata=dict(
type="Element",
namespace="",
min_occurs=0,
max_occurs=9223372036854775807
)
)
description: List[DescriptionType] = field(
default_factory=list,
metadata=dict(
type="Element",
namespace="",
min_occurs=0,
max_occurs=9223372036854775807
)
)
eff_date: str = field(
default="1900-01-01",
metadata=dict(
name="effDate",
type="Attribute"
)
)
other_attributes: Dict[QName, str] = field(
default_factory=dict,
metadata=dict(
type="Attributes",
namespace="##other"
)
)
@dataclass
class ItemsType:
"""
:ivar shirt:
:ivar hat:
:ivar umbrella:
"""
shirt: List[ProductType] = field(
default_factory=list,
metadata=dict(
type="Element",
namespace="",
min_occurs=0,
max_occurs=9223372036854775807
)
)
hat: List[ProductType] = field(
default_factory=list,
metadata=dict(
type="Element",
namespace="",
min_occurs=0,
max_occurs=9223372036854775807
)
)
umbrella: List[ProductType] = field(
default_factory=list,
metadata=dict(
type="Element",
namespace="",
min_occurs=0,
max_occurs=9223372036854775807
)
)
@dataclass
class Items(ItemsType):
class Meta:
name = "items"
XML Document
<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>
xsData XML Document
<items xmlns:oth="http://example.org/oth" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<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 effDate="1900-01-01" oth:custom="12">
<number>557</number>
<name>Ten-Gallon Hat</name>
<color value="red"/>
</hat>
</items>
xsData JSON
{
"shirt": [
{
"number": 557,
"name": "Short-Sleeved Linen Blouse",
"size": [
{
"value": 10,
"system": "US-DRESS"
}
],
"color": [
{
"value": "blue"
}
],
"description": [
{
"www_w3_org_1999_xhtml_element": [
"This shirt is the",
{
"qname": "{http://www.w3.org/1999/xhtml}b",
"text": "best-selling",
"tail": "shirt in\n our catalog!",
"ns_map": {
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xhtml": "http://www.w3.org/1999/xhtml",
"oth": "http://example.org/oth"
},
"children": [],
"attributes": {}
},
{
"qname": "{http://www.w3.org/1999/xhtml}br",
"text": null,
"tail": "Note: runs large.",
"ns_map": {
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
"xhtml": "http://www.w3.org/1999/xhtml",
"oth": "http://example.org/oth"
},
"children": [],
"attributes": {}
}
]
}
],
"eff_date": "2002-04-02",
"other_attributes": {}
}
],
"hat": [
{
"number": 557,
"name": "Ten-Gallon Hat",
"size": [],
"color": [
{
"value": "red"
}
],
"description": [],
"eff_date": "1900-01-01",
"other_attributes": {
"{http://example.org/oth}custom": "12"
}
}
],
"umbrella": []
}
Samples Source
Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR