Designing for XML

This section explains how to design XML Schema objects in the PowerDesigner Object Oriented Model.

XML Object

Modeling in PowerDesigner

Schema

Any package or model can generate an XML Schema. You do not need to define specific attribute or stereotype for the package or model to generate an XML Schema.

Complex type

In XML Schema, a complex type allows elements in its content and may carry attributes. Complex types can be:

  • Global, that is to say defined as a child of the schema element, in order to be reused among schema elements. You design a global complex type using a class with the <<complexType>> stereotype. In the following example, UsAddress is a complex type with a set of attributes that specify it:

    <xsd:complexType name="UsAddress">
      <xsd:element name="name" type="Name"/>
      <xsd:element name="street" type="string"/>
      <xsd:element name="town" type="string"/>
      <xsd:element name="zip" type="Integer"/>
    </xsd:complexType>
  • Local to an element definition. In this case, you have to create a class with the <<element>> stereotype. You then need to set the isComplexType extended attribute of the class to True. This is to make sure that attributes defined in the <<element>> class are generated as a complex type:

    <xsd:element name="customer">
      <xsd:complexType>
      <xsd:element name="name" type="int"/>
      <xsd:element name="address" type="int"/>
      </xsd:complexType>
    </xsd:element>

Simple type

In XML Schema, a simple type can be a string or a decimal built into XML Schema, it can also be a type derived from those built-in the language. A simple type cannot contain elements or attributes. In PowerDesigner, you design a simple type using a class with the <<simpleType>> stereotype. You must add an attribute with the <<simpleType>> stereotype to this class.

<xsd:simpleType name="string">
  <xsd:restriction base="string">
  </xsd:restriction>
</xsd:simpleType>

Deriving types

XML Schema allows you to derive new types by extending or restricting an existing type. In PowerDesigner, you design the extension mechanism using a generalization between two classes. The contentDerivation extended attribute of the generalization allows you to set the type of derivation: extension or restriction.

To design type derivation from a basic type (defined in Settings\DataTypes\BasicDataTypes in the object language editor) you cannot use classes and generalizations, you have to use the simpleContentBase and simpleContentDerivation extended attributes. For example, class A derives from basic data type xsd:string. You define this derivation setting the following values for class A:

Union

A <<union>> class generates a <<union>> attribute and is migrated to specify the attribute location. You can generate single line <union> tag as follows: <union memberTypes=“{member types list}”/>. You can define a value for the extended attribute memberTypes used with simple Type attributes (either <<simpleType>> or <<simpleAttribute>>).

Global element

A global element is declared as a child of the schema element; it can be referenced in one or more declarations. You define an XML Schema global element using a class with the <<element>> stereotype in PowerDesigner. You define the type of a global element using the following methods:

  • For a complex type, use attributes. In this case you have to set the isComplexType extended attribute to True for attributes defined in the <<element>> class to be generated as a complex type:

    <xsd:element name="customer">  
    <xsd:complexType>
    <xsd:element name="name" type="int"/>
    <xsd:element name="address" type="int"/>
    </xsd:complexType>
    </xsd:element>
  • For a simple type, set a value for the type extended attribute

    <xsd:element name="Customer" type="CustomerListType"/>

Element group

An element group is declared as a child of the schema element; it can be referenced in one or more declarations using the <<ref>> stereotype on an association. You define an XML Schema element group using a class with the <<group>> stereotype in PowerDesigner.

Attribute group

An attribute group is a set of attributes. You define an XML Schema attribute group using a class with the <<attributeGroup>> stereotype in PowerDesigner. All the attributes of this class should have the <<attribute>> stereotype. For example, the following attribute group called item gathers information about an item in a purchase order:

<xsd:attributeGroup name="item">
<xsd:attribute name="weight" type="Integer"/>
<xsd:attribute name="shipping_method" type="Integer"/>
</xsd:attributeGroup>

Reference

A reference is a simplified declaration of an element or an attribute group referencing a global definition. In PowerDesigner, you design a reference using an association with the <<ref>> stereotype. In the following example, element Customer references complex type UsAddress.

<xsd:element name="Customer">
<xsd:complexType>
<xsd:element name="name" type="int"/>
<xsd:element name="ID" type="int"/>
<xsd:complexType ref="UsAddress" minOccurs="0"
maxOccurs="unbounded"/>
</xsd:complexType>
</xsd:element>

Note that the referenced element is introduced by its stereotype. In the above example, UsAddress is of <<complexType>> and complexType is displayed in the reference line: <xsd:complexType ref="UsAddress" minOccurs="0" maxOccurs="unbounded"/>

Sequence

XML Schema elements can be constrained to appear in the same order as they are declared, this is called a sequence. In PowerDesigner, depending on the type of sequence you need to design, you can use one of the following methods:

  • If all the class attributes are defined in the sequence, you should create a class without stereotype and set the class extended attribute isSequence to true. In the following example, all attributes of class item_sequence are defined in the sequence:

    <xsd:element name="item_sequence">
    <xsd:sequence>
    <xsd:element name="prodName" type="int"/>
    <xsd:element name="prodID" type="int"/>
    <xsd:element name="prodPrice" type="int"/>
    </xsd:sequence>
    </xsd:element>
  • If some of the class attributes do not belong to the sequence, you have to design the following construct: create a class containing the attributes belonging to the sequence and assign the <<sequence>> stereotype to this class. Create another class containing the other attributes. Select the Inner link tool in the Toolbox and draw a link from the second class to the <<sequence>> class. The resulting code is the following:

    <xsd:element name="PurchaseOrder">
    <xsd:sequence>
    <xsd:element name="shipTo" type="int"/>
    <xsd:element name="billTo" type="int"/>
    </xsd:sequence>
    <xsd:element name="prodID" type="int"/>
    </xsd:element>

By default, inner classes are generated before attributes in a class (as defined in the Class\Template\body entry in the object language definition file). However, you can modify generation order among class attributes using the attribute migration feature. To do so, you should create an association from the parent class to the <<sequence>> class, right-click the association and select Migrate > Migrate Navigable Roles. The migrated attribute can then be moved in the list of class attributes in order to have the desired generation order.

<xsd:element name="PurchaseOrder">
<xsd:element name="prodID" type="int"/>
<xsd:sequence>
<xsd:element name="shipTo" type="int"/>
<xsd:element name="billTo" type="int"/>
</xsd:sequence>
</xsd:element>

Choice & All

A choice allows you to display only one child in an instance of an element. All allows you to display all the elements in the group once or not at all. In PowerDesigner, you design a choice/all mostly as a sequence.

  • If all the class attributes are defined in the choice/all, you should create a class without stereotype and set the class extended attribute isChoice/isAll to true. In the following example, all attributes of class InternationalShipping are defined in the choice:

    <xsd:element name="InternationalShipping">
    <xsd:choice>
    <xsd:element name="EuropeShipping" type="int"/>
    <xsd:element name="AfricaShipping" type="int"/>
    <xsd:element name="AsiaShipping" type="int"/>
    </xsd:choice>
    </xsd:element>
  • If some of the class attributes do not belong to the choice, you have to create a class containing the attributes belonging to the choice and assign the <<choice>>/<<all>> stereotype to this class. Create another class containing the other attributes. Select the Inner link tool in the Toolbox and draw a link from the second class to the <<choice>>>>/<<all>> class: You can also use the attribute migration feature to modify generation order among attributes (see Sequence, above).