#01 - Schemas: An introductionΒΆ
Code Generation
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="product" type="ProductType"/>
<xsd:complexType name="ProductType">
<xsd:sequence>
<xsd:element name="number" type="xsd:integer"/>
<xsd:element name="size" type="SizeType"/>
</xsd:sequence>
<xsd:attribute name="effDate" type="xsd:date"/>
</xsd:complexType>
<xsd:simpleType name="SizeType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="2"/>
<xsd:maxInclusive value="18"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
from dataclasses import dataclass, field
from typing import Optional
from xsdata.models.datatype import XmlDate
@dataclass
class ProductType:
number: Optional[int] = field(
default=None,
metadata={
"type": "Element",
"namespace": "",
"required": True,
}
)
size: Optional[int] = field(
default=None,
metadata={
"type": "Element",
"namespace": "",
"required": True,
"min_inclusive": 2,
"max_inclusive": 18,
}
)
eff_date: Optional[XmlDate] = field(
default=None,
metadata={
"name": "effDate",
"type": "Attribute",
}
)
@dataclass
class Product(ProductType):
class Meta:
name = "product"
Data Binding
<product effDate="2001-04-02"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="chapter01.xsd">
<number>557</number>
<size>10</size>
</product>
<product effDate="2001-04-02">
<number>557</number>
<size>10</size>
</product>
{
"number": 557,
"size": 10,
"eff_date": "2001-04-02"
}
Samples Source
Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR