Chapter 05

Instances and schemas

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 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:
    """
    :ivar product:
    """
    product: List[Product] = field(
        default_factory=list,
        metadata=dict(
            type="Element",
            namespace="http://example.org/prod",
            min_occurs=1,
            max_occurs=9223372036854775807
        )
    )


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


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

XML Document

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

xsData XML Document

<ns0:order xmlns:ns0="http://example.org/ord" xmlns:ns1="http://example.org/prod" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <items>
    <ns1:product>
      <number>557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <size xsi:nil="true"/>
    </ns1:product>
  </items>
</ns0:order>

xsData JSON

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