Thursday, June 11, 2020

XSD - Define a complex type using type attribute or directly by naming

Complex Element is an XML element which can contain other elements and/or attributes. We can create a complex element in two ways −
  • Define a complex type and then create an element using the type attribute
  • Define a complex type directly by naming

Define a Complex Type and then create an element using type attribute.

Here JobHistory element is referring to  JobType complex type using type attribute

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.poc.org" xmlns:ns0="http://www.poc.org" targetNamespace="http://www.poc.org"
            elementFormDefault="qualified">
  <xsd:element name="HREmployeeData">   
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="First" type="xsd:string"/>
        <xsd:element name="Last" type="xsd:string"/>
        <xsd:element name="Phone" type="xsd:string"/>
        <xsd:element name="Income" type="xsd:double"/>
        <xsd:element name="Status" type="xsd:string"/>
        <xsd:element name="JobHistory" type="ns0:JobType"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
    <xsd:complexType name="JobType">
      <xsd:sequence>
        <xsd:element name="JobTitle" type="xsd:string"/>
        <xsd:element name="Salary" type="xsd:double"/>
      </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

Define a complex type directly by naming

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.org"
            targetNamespace="http://www.example.org" elementFormDefault="qualified">
  <xsd:element name="HREmployeeData">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="First" type="xsd:string"/>
        <xsd:element name="Last" type="xsd:string"/>
        <xsd:element name="Phone" type="xsd:string"/>
        <xsd:element name="Income" type="xsd:double"/>
        <xsd:element name="Status" type="xsd:string"/>
        <xsd:element name="JobHistory">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="JobTitle" type="xsd:string"/>
              <xsd:element name="Salary" type="xsd:double"/>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>


No comments:

Post a Comment

Featured Post

11g to 12c OSB projects migration points

1. Export 11g OSB code and import in 12c Jdeveloper. Steps to import OSB project in Jdeveloper:   File⇾Import⇾Service Bus Resources⇾ Se...