SCDJWS Study Guide: XML Schema


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

Declarations

Element Declarations

Elements declarations were mentioned in the Data types section. Those elements are declared as children of a complex type. For example:

<xsd:complexType name="USAddress">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="street" type="xsd:string" />
<xsxd:element name="city" type="xsd:string" />
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN" use="fixed" value="US"/>
</xsd:complexType>

These elements in the example can only be referred to inside the complex type USAddress. In addition to declaraing elements inside a complex type, a schema may also declare global elements, which XML documents can refer to directly. Global elements are declared as direct children of the schema. In the following example, both the person and the comment elements are global:

<xsd:schema xmlns:xsd="http://www.w3.org/2000/08/XMLSchema">
<xsd:element name="person" type="PersonType"/>
<xsd:element name="comment" type="xsd:string"/>
<xsd:complexType name="PersonType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element ref="comment" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

Use the following attributes to declare an element:
"name", "type" defined occurance of a element
"ref" references existing element declared elsewhere in the schema
"minOccurs", "maxOccurs" control number of appearances

The following are all legal examples of element declarations:

<xsd:element name="billTo" type="USAddress" >

<xsd:element ref="comment" minOccurs="0"/>

<xsd:element name="items" type="Items" />

Note: An element declaration can have a type attribute, or a complexType child element, but it cannot have both a type attribute and a complexType child element. The following example is not allowed:

<xsd:element name="A" type="foo">
<xsd:complexType>
......
</xsd:complexType>
</xsd:element>


Attribute Declarations

Attributes declarations, like element declarations, are expressed as elements in an XML schema. The following example shows attributes declarations:

<xsd:complexType name="USAddress">
<xsd:sequence>
<xsd:element name="state" type="xsd:string" />
<xsd:element name="zip" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN" use="fixed" value="US"/>
</xsd:complexType>

To define an attribute:

"use"indicates whether the attribute is required or optional (fixed or default)
"value"provides any value that is called for


Note: An attribute may also have occurrence constraints, but they are different from those of elements. Instead of maxOccurs and minOccurs, attribute types declare the use occurrence constraint, which may be "required", "optional", or "prohibited", indicating that the attribute must, may, or may not be used, respectively. The default is "optional". An attribute may also be declared as "fixed": A fixed value is assigned to the attribute no matter what value appears in the XML instance document.

An attribute may also have a default value, to the assigned if no value is explicitly declared in the XML document. But the default attribute can be used only when the use attribute is "optional". It doesn't make sense when the use is "required" or "prohibited". For if it is "required", the attribute must appear in the XML document. If it is "prohibited", the attribute is not allowed in the XML document.


Sequences and Choices

Sequences define an ordered group of elements, while Choices define a group of mutually exclusive elements or compositors. For example:

<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:choice>
<xsd:group ref="shipAndBill" />
<xsd:element name="singleUSAddress" type=" USAddress" />
</xsd:choice>
<xsd:element ref="comment" minOccurs="0"/>
<xsd:element name="items" type="Items" />
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date" />
</xsd:complexType>
 
<xsd:group name="shipAndBill">
<xsd:sequence>
<xsd:element name="shipTo" type="USAddress" />
<xsd:element name="billTo" type="USAddress" />
</xsd:sequence>
</xsd:group>


Definitions vs. Declarations

  • Definitions create new simple and complex types, for example:

  • <xsd:complexType name="USAddress" >
    <xsd:sequence>
    <xsd:element name="name"type="xsd:string" />
    <xsd:element name="street" type="xsd:string" />
    <xsd:element name="city"� type="xsd:string" />
    <xsd:element name="state"type="xsd:string" />
    <xsd:element name="zip"type="xsd:decimal" />
    </xsd:sequence>
    <xsd:attribute name="country" type="xsd:NMTOKEN"
    use="fixed" value="US"/>
    </xsd:complexType>
  • Declarations are not themselves types

  • Enable elements and attributes

  • <xsd:complexType name="USAddress" >
    <xsd:sequence>
    <xsd:element name="name"type="xsd:string" />
    <xsd:element name="street" type="xsd:string" />
    <xsd:element name="city"type="xsd:string" />
    <xsd:element name="state"type="xsd:string" />
    <xsd:element name="zip"type="xsd:decimal" />
    </xsd:sequence>
    <xsd:attribute name="country" type="xsd:NMTOKEN"use="fixed" value="US"/>
    </xsd:complexType>

The advantage of defining USAddress' element declarations and wrapping them in a named type is that now this type can be reused by other elements.

Please note that:

<xsd:element name="A" type="foo"/>
<xsd:complexType name="foo">
<xsd:sequence>
<xsd:element name="B" .../>
<xsd:element name="C" .../>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

is equivalent to:


<xsd:element name="A">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="B" .../>
<xsd:element name="C" .../>
</xsd:sequence>
</xsd:complexType>
</xsd:element>


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |