Defining object type attributes

Build 1501 on 14/Nov/2017  This topic last edited on: 5/Aug/2014, at 15:05

There are two ways in which object type attributes are defined in the schema:

Define schema attributes

Define sub-elements of the complex type

Defining schema attributes

 <xs:element name="test">

   <xs:complexType>

     <xs:complexContent>

       <xs:extension base="object">

        <xs:attribute 

            name="location" 

            type="xs:string"/>

       </xs:extension>

     </xs:complexContent>

   </xs:complexType>

 </xs:element>

 

This defines an object type ‘test’ with a (string) attribute ‘location’.

Mandatory attributes and defaults

To specify if the attribute is compulsory or optional and to specify the default value, use the ‘use’ and ‘default’ options respectively:

   <xs:attribute

    name="location"

    type="xs:string"

    use="required

      default="Milan"/>

Defining sub-elements of the complex type

An alternative way to define object type attributes is to define sub-elements of the complex type:

 <xs:element name="test">

   <xs:complexType gs:key="name">

     <xs:complexContent>

       <xs:extension base="object">

         <xs:sequence>

          <xs:element 

              name="location" 

              type="xs:string"/>

       </xs:extension>

     </xs:complexContent>

   </xs:complexType>

 </xs:element>

 

This defines an object type ‘test’ with a (string) attribute ‘location’ – exactly the same thing as the previous example. The difference is in the XML representation of the objects.

In the first case the XML will be:

<test location="Milan" >

</test>

and in the second case it will be:

<test>

  <location>Milan</location>

</test>

Which one to choose in this case is a matter of preference – there is no difference in terms of the database structure. For some attributes though it is necessary to use always one of the two systems.

See also

Attributes