Chapter 03

Namespaces

Binding Test

Schema

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:ord="http://example.org/ord">

  <xsd:annotation>
    <xsd:documentation>
      This provides an example of validating an instance with multiple namespaces.
      chapter03env.xsd imports chapter03ord.xsd, which imports chapter03prod.xsd, which
      imports chapter03prod2.xsd.  The "form" attribute is used to indicate whether
      element-type and/or attribute names should be qualified in the instance.
    </xsd:documentation>
  </xsd:annotation>

  <xsd:import namespace="http://example.org/ord" schemaLocation="chapter03ord.xsd"/>

  <xsd:element name="envelope" type="EnvelopeType"/>

  <xsd:complexType name="EnvelopeType">
    <xsd:sequence>
      <xsd:element ref="ord:order" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>

</xsd:schema>
from dataclasses import dataclass, field
from typing import List
from tests.fixtures.defxmlschema.chapter03ord import (
    Order,
)


@dataclass
class EnvelopeType:
    """
    :ivar order:
    """
    order: List[Order] = field(
        default_factory=list,
        metadata=dict(
            type="Element",
            namespace="http://example.org/ord",
            min_occurs=1,
            max_occurs=9223372036854775807
        )
    )


@dataclass
class Envelope(EnvelopeType):
    class Meta:
        name = "envelope"

XML Document

<envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="chapter03.xsd">
  <order  xmlns="http://example.org/ord"
          xmlns:prod="http://example.org/prod">
    <number>123ABBCC123</number>
    <items>
      <product xmlns="http://example.org/prod">
        <number prod:id="prod557">557</number>
        <name xmlns="">Short-Sleeved Linen Blouse</name>
        <prod:size system="US-DRESS">10</prod:size>
        <prod:color xmlns:prod="http://example.org/prod2"
                    prod:value="blue"/>
      </product>
    </items>
  </order>
</envelope>

xsData XML Document

<envelope xmlns:ns0="http://example.org/ord" xmlns:ns1="http://example.org/prod" xmlns:ns2="http://example.org/prod2">
  <ns0:order>
    <ns0:number>123ABBCC123</ns0:number>
    <ns0:items>
      <ns1:product>
        <ns1:number ns1:id="prod557">557</ns1:number>
        <name>Short-Sleeved Linen Blouse</name>
        <ns1:size system="US-DRESS">10</ns1:size>
        <ns2:color ns2:value="blue"/>
      </ns1:product>
    </ns0:items>
  </ns0:order>
</envelope>

xsData JSON

{
    "order": [
        {
            "number": "123ABBCC123",
            "items": {
                "product": [
                    {
                        "number": {
                            "value": 557,
                            "id": "prod557"
                        },
                        "name": "Short-Sleeved Linen Blouse",
                        "size": {
                            "value": 10,
                            "system": "US-DRESS"
                        },
                        "color": {
                            "value": "blue"
                        }
                    }
                ]
            }
        }
    ]
}

Samples Source

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