Home > Articles > Web Services > XML

  • Print
  • + Share This
From the author of

Restricting Complex Types

The restriction element provides functionality for deriving a complex type that restricts a base complex type. For simple content, these restrictions might only specify constraining facets that apply to simple content or to the attribute types. For complex content, the restriction permits altering attributes of nested element types or attribute types.

The next element describes a restriction of the partOptionType complex type presented in the all element type section. Specifically, certain parts do not require a size:



<xsd:complexType name="noColorPartOptionType">
  <xsd:complexContent>
    <xsd:restriction base="partOptionType">
      <xsd:all>
        <xsd:element name="color"
               type="xsd:token"/>
        <xsd:element name="size"
               type="xsd:token"
               minOccurs="0"
               maxOccurs="0"/>
      </xsd:all>
    </xsd:restriction>
  </xsd:complexContent>
</xsd:complexType>

An element type whose structure type is noColorPartOptionType might look like the following element:

<xsd:element name="noColorPartOption"
       type="noColorPartOptionType"/>

Given the preceding element type, the following element, which permits and requires only color, is valid in an XML instance:

<noColorPartOption>
  <color>aquamarine</color>
<noColorPartOption>

Note the line containing 'maxOccurs = "0"'. This restriction enforces that despite the fact that the partOptionType specifies an optional size, the derived noColorPartOptionType specifies that size must not exist.

  • + Share This
  • 🔖 Save To Your Account