XSD generated type examples

These examples demonstrate how to use generated types.

XSD generated type examples 1

Create myschema.xsd in the package examples.pl.gosu.xml:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="Element1">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name= "Child1" /> <!-- default type is xsd:anyType -->  
        <xsd:element name="Child2" type="xsd:int"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Review the following Gosu code:

uses examples.pl.gosu.xml.myschema.Element1
var xml = Element1()
var child1 = xml.Child1          // Child1 has type anyType (the default when a type is not specified).
var child2 = xml.Child2          // Child2 has type java.lang.Integer.
xml.Child1.$Text = "a string"      // Set the XML property as a text string
xml.Child2 = 5                     // Set the XML property with a simple type. 
var child2Elem = xml.Child2_elem // Get this lower-level element as
                                   // myschema.anonymous.elements.Element1_Child2.
xml.print()                        // Display the entire element
child2Elem.print()                 // Display the Child2 lower-level element

Note the following:

  • The Child1 property is of type schema.anonymous.elements.Element1_Child1, which is a subclass of XmlElement.
  • The Child2 property is of type java.lang.Integer. When a child element has a simple type, its natural property name gets the object’s value, rather than the child element object. If you wish to access the element object (the XmlElement instance) for that child, instead use the property with the _elem suffix. In this case, for the child named Child2, you use the element.Child2_elem property, which is of type schema.anonymous.elements.Element1_Child2.

XSD generated types: element type and backing type instances

The following XSD defines a phone complex type, as well as multiple elements using that complex type:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="person">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="cell" type="phone"/>
        <xsd:element name="work" type="phone"/>
        <xsd:element name="home" type="phone"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:complexType name="phone">
    <xsd:sequence>
      <xsd:element name="areaCode" type="xsd:string"/>
      <xsd:element name="mainNumber" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

This XSD supports multiple ways to create and assign the phone numbers.

The following code creates three different phone numbers:

var ex = new schema.Person()

ex.Cell.AreaCode = "415"
ex.Cell.MainNumber = "555-1213"

ex.Work.AreaCode = "416"
ex.Work.MainNumber = "555-1214"

ex.Home.AreaCode = "417"
ex.Home.MainNumber = "555-1215"

The following code creates one phone number used in multiple elements:

uses examples.pl.gosu.xml.myschema.Person
uses examples.pl.gosu.xml.myschema.types.complex.Phone

var ex = new Person()

var c = new Phone()
var w = new Phone()
var h = new Phone()

c.AreaCode = "415"
c.MainNumber = "555-1212"
w.AreaCode = "510"
w.MainNumber = "555-1213"
h.AreaCode = "408"
h.MainNumber = "555-1214"

ex.Cell.$TypeInstance = c // use the $TypeInstance property for assignment
ex.Work.$TypeInstance = w
ex.Home.$TypeInstance = h
Note: An element’s $TypeInstance property is required to access the element’s backing type instance.

For example, you cannot create the complex type and directly assign it to the element type:

ex.Cell = c // SYNTAX ERROR: cannot assign complex type instance to element type instance
ex.Work = w // SYNTAX ERROR: cannot assign complex type instance to element type instance
ex.Home = h // SYNTAX ERROR: cannot assign complex type instance to element type instance

Additionally, different element-based types can be mutually incompatible for assignment, even if they are associated with the XSD type definition. For example:

var ex = new Person()
ex.Cell = ex.Work // SYNTAX ERROR: cannot assign one element type to a different element type