Chapter 12: Complex typesΒΆ

Schema

<!--Definitive XML Schema by Priscilla Walmsley (c) 2012 Prentice Hall PTR -->
<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>

XML Document

<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="../../../../../xsdata-samples/dxsfullexamples/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

<?xml version='1.0' encoding='UTF-8'?>
<items xmlns:ns0="http://www.w3.org/1999/xhtml">
  <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<ns0:b>best-selling</ns0:b>shirt in
      our catalog!<ns0:br/>Note: runs large.</description>
  </shirt>
  <hat xmlns:ns0="http://example.org/oth" effDate="1900-01-01" ns0: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!",
                            "children": [],
                            "attributes": {}
                        },
                        {
                            "qname": "{http://www.w3.org/1999/xhtml}br",
                            "text": null,
                            "tail": "Note: runs large.",
                            "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