#05 - Instances and schemasΒΆ

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 example shows how xsi:schemaLocation can be used to pull together multiple schema documents.
      An import is used to show the dependence on the prod namespace, but no schema location
      is provided in the schema; this type of "dangling" reference to product is allowed.
      This example also exhibits xsi:nil and xsi:type.
    </xsd:documentation>
  </xsd:annotation>

  <xsd:import namespace="http://example.org/prod" schemaLocation="chapter05prod.xsd"/>

  <xsd:element name="order" type="OrderType"/>
  <xsd:complexType name="OrderType">
    <xsd:sequence>
      <xsd:element name="items" type="ItemsType"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="ItemsType">
    <xsd:sequence>
      <xsd:element ref="prod:product" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>


</xsd:schema>
from dataclasses import dataclass, field
from typing import List, Optional
from tests.fixtures.defxmlschema.chapter05prod import Product

__NAMESPACE__ = "http://example.org/ord"


@dataclass
class ItemsType:
    product: List[Product] = field(
        default_factory=list,
        metadata={
            "type": "Element",
            "namespace": "http://example.org/prod",
            "min_occurs": 1,
        }
    )


@dataclass
class OrderType:
    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:prod="http://example.org/prod"
           xmlns:xsd="http://www.w3.org/2001/XMLSchema"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://example.org/prod chapter05prod.xsd
                               http://example.org/ord chapter05.xsd">
  <items>
    <prod:product>
      <number xsi:type="xsd:short">557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <size xsi:nil="true"></size>
    </prod:product>
  </items>
</ord:order>
<ns0:order xmlns:ns0="http://example.org/ord">
  <items>
    <ns1:product xmlns:ns1="http://example.org/prod">
      <number>557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <size xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
    </ns1:product>
  </items>
</ns0:order>
{
    "items": {
        "product": [
            {
                "number": 557,
                "name": "Short-Sleeved Linen Blouse",
                "size": {
                    "value": null,
                    "system": null
                }
            }
        ]
    }
}

Samples Source

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