Defining simpleTypes in XML Schemas

Many simpleTypes, such as string and integer, are built in to an XML Schema. You can define your own simpleType by restricting the range of values provided by a built-in simpleType. You can also define simpleTypes that are derived from the simpleTypes you define.

This section covers the following topics:

About simpleTypes in XML Schemas

XML Schema defines several kinds of simpleTypes:

  • Atomic is the term for most of the simpleTypes built in to XML Schema, such as integer, string, and decimal. They are called "atomic" because in the context of XML Schema, no part of an element or attribute of an atomic type has meaning on its own. It is only the whole instance that has meaning.
  • Descriptions of the XML Schema built-in types are in the W3C XML Schema Part 2: Datatypes.

  • List simpleTypes are sequences of atomic types. All elements of a particular list simpleType are instances of the same atomic type.
  • Union simpleTypes are sequences of atomic and list types. However, the elements of a particular union simpleType can be instances of more than one atomic or list type.
  • Anonymous simpleTypes are simpleType definitions that are not explicitly named. An anonymous simpleType can be an atomic, list, or union simpleType. You define an anonymous simpleType in the element or attribute definition that uses it.
  • Anonymous simpleTypes are useful when you want to define a type that is used in only one element or attribute. By specifying an anonymous simpleType, you save the overhead of explicitly defining the type and specifying a reference to it.

Examples of simpleTypes in an XML Schema

The W3C XML Schema Part 0: Primer specifies the following simpleType in its sample purchase order schema:

<!-- Stock Keeping Unit, a code for identifying products -->
               
<xsd:simpleType name="SKU"> 
               

              
<xsd:restriction base="xsd:string"> 
                   
<xsd:pattern value="\d{3}-[A-Z]{2}"/> 
                   
</xsd:restriction> 
                   

                
</xsd:simpleType>

This specifies that SKU is a simpleType. It is restricted to the values of the base type, which is xsd:string. This means that for a node that is of type SKU, the possible values are a subset of the values allowed for the xsd:string type.

The xsd:pattern element specifies that the pattern facet is being applied to the set of values allowed by the xsd:string type. The value of the xsd:pattern element is an XML Schema regular expression that specifies the allowable values for nodes of type SKU. In this example, the regular expression specifies that the value must be three digits, followed by a hyphen, followed by two uppercase ASCII letters - <xsd:pattern value="\d{3}-[A-Z]{2}"/>. For information about XML Schema expressions, see the W3C XML Schema Part 0: Primer.

Elsewhere in the purchase order schema, an attribute definition specifies that SKU is the type of its value:

<xsd:attribute name="partNum" type="SKU" use="required"/>
               

            

An XML document that uses a schema that contains this simpleType definition can specify the partNum attribute. The parser ensures that the value of the partNum attribute is in the range specified by the xsd:pattern element. The SKU type itself is not mentioned in the instance document.

Following is another example of a simpleType definition from the W3C XML Schema Part 0: Primer. This simpleType, myInteger, is based on the xsd:integer type. It specifies two facets ( minInclusive and maxInclusive), which specify the lower and upper inclusive bounds of the range of valid values.

<xsd:simpleType name="myInteger">
               

              
  <xsd:restriction base="xsd:integer">
                   
    <xsd:minInclusive value="10000"/>
                   
    <xsd:maxInclusive value="99999"/>
                   
  </xsd:restriction>
                   

                
</xsd:simpleType>

Defining a simpleType in the Diagram View

This section describes the procedures for defining simpleTypes in the Diagram view. It covers the following topics:

Before You Begin

Many of the editing features used in this section are described in Defining an XML Schema Using the Diagram Tab - Getting Started. You should familiarize yourself with that material if you have not done so already.

Defining an Atomic simpleType

This topic provides the steps for defining an atomic simpleType in the Diagram view.

In the Diagram view, to define an atomic simple type:
1. Right-click the schema node to display the shortcut menu.
2. Select Add > simpleType.

The new simpleType appears in the diagram; its properties are displayed in the Properties window.

3. Change the default name to the name of the new simpleType and press Enter.

Specifying a Restriction for a simpleType - QuickEdit

QuickEdit is a feature that combines commonly-performed editing operations, such as specifying a restriction for a simpleType. You can also perform this operation in a different way. See Specifying a Restriction for a simpleType - Manually.

To specify a restriction for a simple type using QuickEdit:
1. Right-click the simpleType node to display the shortcut menu.
2. Select QuickEdit > Derive by restriction from the shortcut menu.

The Type Derivation dialog box appears.

Figure 246. Type Derivation Dialog Box

The Type Derivation dialog box displays the W3C XML Schema, as well as any referenced XML Schemas.

3. Expand the schema (click the plus sign) to display the base types associated with that XML Schema.
4. Select the type on which you wish to base the simpleType you are defining and click OK.

The simpleType is updated with an element that identifies the restricted type:

Figure 247. Restricted Type

In the lower half of the Properties window, Stylus Studio displays a section that allows you to specify facets - values that define the constraint on the range of values allowed by the base type.

5. Click the Name field and select a facet type.

Figure 248. Specifying Facets for a Restricted Type

Stylus Studio displays only those facets that are allowed for the base type you selected. For a description of each facet, see About Facet Types for simpleTypes.

6. Click the Value field for a facet you want to specify.
7. Enter a value for the new facet.
8. To specify another facet, repeat step 5 through step 7.

Specifying a Restriction for a simpleType - Manually

This procedure describes how to specify a restriction for a simpleType manually. It is an alternative to the procedure described in Specifying a Restriction for a simpleType - QuickEdit.

To specify a restriction for a simple type manually:
1. Right-click the simpleType node to display the shortcut menu.
2. Select Add > Restriction from the shortcut menu.

The simpleType is updated with a restriction icon:

Figure 249. Restriction Icon

3. Select the restriction icon if it is not already selected.
4. In the Properties window, select the type on which you want to base the simpleType you are defining from the Base Type field.

At the bottom of the Properties window, Stylus Studio displays a section that allows you to specify facets - values that define the constraint on the range of values provided by the base type.

5. Click the Name field and select a facet type.

Figure 250. Specifying Facets for a Restricted Type

Stylus Studio displays only those facets that are allowed for the base type you selected. For a description of each facet, see About Facet Types for simpleTypes.

6. Click the Value field for a facet you want to specify.
7. Enter a value for the new facet.
8. To specify another facet, repeat step 5 through step 7.

Defining List and Union simpleTypes

The procedure for defining list and union simple types is similar:

1. Create the simpleType as described in Defining a simpleType in the Diagram View.
2. Select the type (list or union) from the shortcut menu (right-click the new simpleType and select Add > List or Add > Union).
3. Specify the nodes that comprise the simpleType's list or union. These types are restricted to annotations and other simpleTypes.

How you perform this last step depends on whether you are adding new or existing annotation or simpleType nodes to the list or union.

  • To add a new, undefined annotation or simpleType to the list or union, right click the list or union and select Add > Annotation or Add > SimpleType from the shortcut menu.
  • To add an existing annotation or simpleType to the list or union, drag the annotation or simpleType to the list or union, and drop it there, as shown in Figure 251, which shows SimpleType-6 being added to the union SimpleType-3.

Figure 251. Dragging Nodes to Define Other Nodes

Notice that the pointer changes shape when you place it over an appropriate target node.

Defining a simpleType in the Tree View

This topic provides the steps for defining an atomic simpleType in the Tree view. When you are familiar with this procedure, you can adapt it to define list, union, and anonymous simpleTypes.

In the Tree view, to define an atomic simple type:
1. Click the node you want to define a simpleType for. This can be one of the following types of nodes:
    • schema
    • element
    • attribute
    • list
    • union
    • To define a simpleType as the sibling of another node, click the node and hold down the Shift key when you click the button in step 2. You cannot, of course, define a simpleType as a sibling of the Schema node.

2. In the left tool bar, click New simpleType . Stylus Studio displays an empty simpleType field as the last child of the node you selected. If you held down the Shift key, the field is the last sibling of the selected node.
3. Type a name for the new simpleType and press Enter.
4. In the left tool bar, click New Restriction . A restriction specifies the type that the new simpleType is derived from. This is the base type.

Stylus Studio displays a scrollable list of XML Schema built-in types, and any simpleTypes you already defined in this schema. Descriptions of the XML Schema built-in types are in the W3C XML Schema Part 0: Primer.

5. Double-click the simpleType that you want to base your new simpleType on.
6. In the left tool bar, click New Facet . A facet specifies a constraint on the range of values provided by the base type. Stylus Studio displays a scrollable list of XML Schema facet types. For a description of each facet, see About Facet Types for simpleTypes.

You must ensure that you specify a facet that is valid for the specified base type. Stylus Studio does not prevent you from specifying an invalid facet. The W3C XML Schema Part: 0 Primer includes a table that provides this information.

7. Double-click the type of facet you want to specify.
8. In the Properties window, double-click the Value field.
9. Enter a value for the new facet.
10. To add another facet, click the restriction node for your simpleType, and repeat step 6 through step 9.

About Facet Types for simpleTypes

Table 36 provides a brief description of what you should specify as the value of a facet for a new simpleType. You should consult the XML Schema Recommendation for a complete definition of each facet and its allowable values.

Facet
Value
enumeration
One allowable value. Add an enumeration facet for each allowable value. For example:
<xsd:simpleType name="USState">
               

              
  <xsd:restriction base="xsd:string">
                   
    <xsd:enumeration value="AK"/>
                   
    <xsd:enumeration value="AL"/>
                   
    <xsd:enumeration value="AR"/>
                   
    <!-- and so on ... -->
                   
  </xsd:restriction>
                   

                
</xsd:simpleType>
fractionDigits
The maximum number of digits that are allowed in the fractional portion of values of simpleTypes that are derived from xsd:decimal.
length
The number of units of length. Units vary according to the base type. The simpleType must be this number of units of length. For example, if xsd:string is the base type, you might specify 5 as the length if you know that each value will be a code that always has five characters.
maxExclusive
The exclusive upper bound of the range of values allowed for this simpleType. The value of the simpleType must be less than the value of maxExclusive.
maxInclusive
The inclusive upper bound of the range of values allowed for this simpleType. The value of the simpleType must be less than or equal to the value of maxInclusive.
maxLength
The maximum number of units of length. Units vary according to the base type. The length of the instances of this simpleType must be less than or equal to this number of lengths.
minExclusive
The exclusive lower bound of the range of values allowed for this simpleType. The value of the simpleType must be more than the value of minExclusive.
minInclusive
The inclusive lower bound of the range of values allowed for this simpleType. The value of the simpleType must be equal to or more than the value of minInclusive.
minLength
The minimum number of units of length. Units vary according to the base type. The length of the instances of this simpleType must be equal to or more than this number of lengths.
pattern
A regular expression. The values of the simpleType must be literals that match this regular expression.
totalDigits
The maximum number of digits that are allowed in values of simpleTypes that are derived from xsd:decimal.
whiteSpace
Specify one of the following values:
  • preserve indicates that no normalization is done. The value is not changed.
  • replace indicates that each tab, line feed, and return is replaced with a space.
  • collapse indicates that the processing specified by replace is done, and then contiguous sequences of spaces are collapsed into one space.
Table 36. Facet Values for simpleTypes

Defining List and Union simpleTypes in the Tree View

Sometimes you need to define a simpleType for a sequence of atomic types. In a list simpleType, all instances in the sequence must be of the same type. In a union simpleType, the instances in the sequence can be of different types. The procedure for defining list and union simpleTypes is the same.

In the Tree view, to define a list or union simple type:
1. Click the node you want to define the list or union type for.
2. In the left tool bar, click New simpleType . Stylus Studio displays an empty simpleType field as the last child of the node you selected.
3. Type a name for the new simpleType and press Enter.
4. In the left tool bar, click New Aggregator . Stylus Studio displays a pop-up menu with two choices.
5. Double-click list or union.
6. Define the atomic simpleType of the elements or attributes that are instances of the list or union type. See Defining simpleTypes in XML Schemas.
7. If you are defining a list type you are done. If you are defining a union type, click the union node and define another atomic simpleType that can be in the union. Perform this step for each type in the union.
 
Free Stylus Studio XML Training: