Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.此内容没有您所选择的语言版本。
2.5. Defining complex data types
Abstract
2.5.1. Defining data structures 复制链接链接已复制到粘贴板!
Overview 复制链接链接已复制到粘贴板!
complexType elements. Specifying a complex type requires three pieces of information:
- The name of the defined type is specified in the
nameattribute of thecomplexTypeelement. - The first child element of the
complexTypedescribes the behavior of the structure’s fields when it is put on the wire. See the section called “Complex type varieties”. - Each of the fields of the defined structure are defined in
elementelements that are grandchildren of thecomplexTypeelement. See the section called “Defining the parts of a structure”.
Example 2.3. Simple Structure
struct personalInfo
{
string name;
int age;
};
struct personalInfo
{
string name;
int age;
};
Example 2.4. A complex type
Complex type varieties 复制链接链接已复制到粘贴板!
complexType element determines which variety of complex type is being used. Table 2.1, “Complex type descriptor elements” shows the elements used to define complex type behavior.
sequence element, an all element, or a choice is not specified, then a sequence is assumed. For example, the structure defined in Example 2.4, “A complex type” generates a message containing two elements: name and age.
choice element, as shown in Example 2.5, “Simple complex choice type”, it generates a message with either a name element or an age element.
Example 2.5. Simple complex choice type
Defining the parts of a structure 复制链接链接已复制到粘贴板!
element elements. Every complexType element should contain at least one element element. Each element element in the complexType element represents a field in the defined data structure.
element elements have two required attributes:
name and type, element elements have two other commonly used optional attributes: minOcurrs and maxOccurs. These attributes place bounds on the number of times the field occurs in the structure. By default, each field occurs only once in a complex type. Using these attributes, you can change how many times a field must or can appear in a structure. For example, you can define a field, previousJobs, that must occur at least three times, and no more than seven times, as shown in Example 2.6, “Simple complex type with occurrence constraints”.
Example 2.6. Simple complex type with occurrence constraints
minOccurs to make the age field optional by setting the minOccurs to zero as shown in Example 2.7, “Simple complex type with minOccurs set to zero”. In this case age can be omitted and the data will still be valid.
Example 2.7. Simple complex type with minOccurs set to zero
Defining attributes 复制链接链接已复制到粘贴板!
complexType element name is an attribute. They are specified using the attribute element. It comes after the all, sequence, or choice element and are a direct child of the complexType element. Example 2.8, “Complex type with an attribute” shows a complex type with an attribute.
Example 2.8. Complex type with an attribute
attribute element has three attributes:
default. The default attribute allows you to specify a default value for the attribute.
2.5.2. Defining arrays 复制链接链接已复制到粘贴板!
Overview 复制链接链接已复制到粘贴板!
maxOccurs attribute has a value greater than one. The second is to use SOAP arrays. SOAP arrays provide added functionality such as the ability to easily define multi-dimensional arrays and to transmit sparsely populated arrays.
Complex type arrays 复制链接链接已复制到粘贴板!
maxOccurs attribute. For example, to define an array of twenty floating point numbers you use a complex type similar to the one shown in Example 2.9, “Complex type array”.
Example 2.9. Complex type array
<complexType name="personalInfo"> <element name="averages" type="xsd:float" maxOccurs="20"/> </complexType>
<complexType name="personalInfo">
<element name="averages" type="xsd:float" maxOccurs="20"/>
</complexType>
minOccurs attribute.
SOAP arrays 复制链接链接已复制到粘贴板!
wsdl:arrayType element. The syntax for this is shown in Example 2.10, “Syntax for a SOAP array derived using wsdl:arrayType”.
Example 2.10. Syntax for a SOAP array derived using wsdl:arrayType
[]; to specify a two-dimensional array use either [][] or [,].
wsdl:arrayType attribute specifies the type of the array elements, xsd:string, and the number of dimensions, with [] implying one dimension.
Example 2.11. Definition of a SOAP array
Example 2.12. Syntax for a SOAP array derived using an element
maxOccurs attribute must always be set to unbounded.
2.5.3. Defining types by extension 复制链接链接已复制到粘贴板!
alienInfo, that extends the personalInfo structure defined in Example 2.4, “A complex type” by adding a new element called planet.
- The name of the type is defined by the
nameattribute of thecomplexTypeelement. - The
complexContentelement specifies that the new type will have more than one element.NoteIf you are only adding new attributes to the complex type, you can use asimpleContentelement. - The type from which the new type is derived, called the base type, is specified in the
baseattribute of theextensionelement. - The new type’s elements and attributes are defined in the
extensionelement, the same as they are for a regular complex type.
alienInfo is defined as shown in Example 2.13, “Type defined by extension”.
Example 2.13. Type defined by extension
2.5.4. Defining types by restriction 复制链接链接已复制到粘贴板!
Overview 复制链接链接已复制到粘贴板!
SSN, which is a string of exactly nine characters. New types defined by restricting simple types are defined using a simpleType element.
- The name of the new type is specified by the
nameattribute of thesimpleTypeelement. - The simple type from which the new type is derived, called the base type, is specified in the
restrictionelement. See the section called “Specifying the base type”. - The rules, called facets, defining the restrictions placed on the base type are defined as children of the
restrictionelement. See the section called “Defining the restrictions”.
Specifying the base type 复制链接链接已复制到粘贴板!
restriction element. The restriction element is the only child of a simpleType element and has one attribute, base, that specifies the base type. The base type can be any of the XML Schema simple types.
Example 2.14. Using int as the base type
<simpleType name="restrictedInt">
<restriction base="xsd:int">
...
</restriction>
</simpleType>
<simpleType name="restrictedInt">
<restriction base="xsd:int">
...
</restriction>
</simpleType>
Defining the restrictions 复制链接链接已复制到粘贴板!
value, that defines how the facet is enforced. The available facets and their valid value settings depend on the base type. For example, xsd:string supports six facets, including:
lengthminLengthmaxLengthpatternwhitespaceenumeration
restriction element.
Example 复制链接链接已复制到粘贴板!
SSN, which represents a social security number. The resulting type is a string of the form xxx-xx-xxxx. <SSN>032-43-9876<SSN> is a valid value for an element of this type, but <SSN>032439876</SSN> is not.
Example 2.15. SSN simple type description
<simpleType name="SSN">
<restriction base="xsd:string">
<pattern value="\d{3}-\d{2}-\d{4}"/>
</restriction>
</simpleType>
<simpleType name="SSN">
<restriction base="xsd:string">
<pattern value="\d{3}-\d{2}-\d{4}"/>
</restriction>
</simpleType>
2.5.5. Defining enumerated types 复制链接链接已复制到粘贴板!
Overview 复制链接链接已复制到粘贴板!
enumeration facet which is supported by all XML Schema primitive types. As with enumerated types in most modern programming languages, a variable of this type can only have one of the specified values.
Defining an enumeration in XML Schema 复制链接链接已复制到粘贴板!
Example 2.16. Syntax for an enumeration
Example 复制链接链接已复制到粘贴板!
widgetSize, shown in Example 2.17, “widgetSize enumeration”, would be valid if it contained <widgetSize>big</widgetSize>, but it would not be valid if it contained <widgetSize>big,mungo</widgetSize>.
Example 2.17. widgetSize enumeration