Adding XSD restrictions on values in UML models

You can add a restriction for a simple or complex type in a UML model, and then run a UML-to-XSD transformation to generate an XML schema that contains simple or complex types with their possible values restricted to match a pattern that a regular expression specifies. You can also run an XSD-to-UML transformation on an XSD schema containing a type with a restriction to generate these elements in the model.

Before you begin

You must apply the XSD transformation profile to your model, and your model must contain a package with the «schema» stereotype.

About this task

The source class in the model can be a simple or complex type that has a generalization relationship to another simple or complex type, respectively. When you run a UML-to-XSD transformation on the source model, if a generalization between simple types has no stereotype, it is still generated as an XSD restriction in the XML schema, because simple types cannot have an extension as a child element. If a generalization between complex types has no stereotype, it is generated as an XSD extension in the XML schema. If the same generalization has the «restriction» stereotype, it is generated as an XSD restriction.

When you run an XSD-to-UML transformation on an XML schema, XSD restrictions generate simple or complex types with a generalization relationship to another type of the same kind. The generalization relationship is generated with a «restriction» stereotype.

To restrict a type named Class1 in a package named Package1:

Procedure

  1. In the Project Explorer view, add a class to the schema package; then apply the «simpleType» or «complexType» stereotype to the class. For example, add a class named Class1 to the schema package.
  2. Edit the UML properties for Class1 to create a generalization relationship:
    1. Right-click Class1; then click UML Properties.
    2. Click Generalization.
    3. In the upper-right corner of the Properties window, click Insert New Generalization, which is represented by a button with an arrow icon.
    4. In the Select Element window, browse the model, expand the model element, and then expand (XSDDataTypes) > XSDDataTypes.
    5. Select a data type. For example, select string.
    6. Click OK.
    7. Click Close.

    The following figure shows the Properties window in the background and the Select Element window in the foreground, which has been opened create a generalization relationship.

    The screen capture shows the Properties window in the background with the Select Element open in the foreground and the project tree is expanded to display the XSDDataTypes.

  3. In the Project Explorer view, expand the class named Class1, and select the new generalization relationship.
  4. In the Properties view, click the Stereotypes tab; then click Apply Stereotype and select restriction. The following figure shows a diagram view of the new model elements that have been created.

    The screen capture shows a diagram view of Class1 stereotyped simpleType with a generalization relationship stereotyped restriction to a string type.

  5. Select the class named Class1; then, in the Properties view, click the Stereotypes tab.
  6. Under the Stereotype Properties section, select the pattern property and type a value to define the restriction. For example, type [0-9]{5}(-[0-9]{4})? as the value for pattern as the following figure shows.

    The screen capture shows the Properties view with the Stereotypes tab selected and the pattern property is highlighted.

Example

After you configure and run a UML-to-XSD transformation with Package1 as the source, the example model generates an XML schema similar to the following XML code:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:Package1="http://Package1/" targetNamespace="http://Package1/">
  <xsd:simpleType name="Class1">
    <xsd:restriction base="xsd:string">
      <xsd:pattern value="[0-9]{5}(-[0-9]{4})?"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>

Feedback