|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TODAY'S TOP SOA & WEBSERVICES LINKS Feature SimpleType and ComplexType in a Schema
Designing your own
By: Deepak Vohra; Ajay Vohra
Oct. 1, 2004 12:00 AM
This tutorial discusses the simpleType and complexType XML Schema structures and their corresponding representations in an XML document. XML Schema is used as the basis of an XML document structure, and some of the XML technologies, such as JAXB, are based on XML Schema. Overview SimpleType Declaration <xs:schema> or <xs:element name="A"> With an attribute declaration, the simpleType is used as <complexType> <xs:attribute name="AttributeA" type="simpleTypeA"/> </complexType> or <xs:attribute name="AttributeA"> The <xs:simpleType/> declaration may be present in an <xs:element/> element, <xs:attribute/> element, <xs:restriction/> element, <xs:list/> element, <xs:union/> element, or the <xs:schema/> element. The <xs:simpleType/> element may contain one of the <xs:restriction/>, <xs:list/>, <xs:union/> elements. SimpleType with Restriction Element <xs:element name="A"> A corresponding representation of element Element "A" in an XML document would be <A>5.0</A> Element "A" in an XML document may have values between 2.00 and 5.00 (inclusive). Declaration <A>1.0</A> or <A>25.0</A> would not validate with the Schema. A simpleType to constrain the length of a string data type would be <xs:element name="A"> A corresponding representation of "A" in an XML document would be <A>string</A> or <A>stringelement</A> would not validate with the Schema. An element with a fixed length would be represented with simpleType as <xs:element name="A"> An example of a corresponding representation of ?A? in an XML document would be <A>strin</A> An attribute with an enumeration of values would be represented with simpleType as
<xs:element name="A" >
<xs:complexType>
<xs:attribute
name="AttributeA">
<xs:simpleType>
<xs:restriction
base="xs:string">
<xs:enumeration
value="a"/>
<xs:enumeration
value="b"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
A corresponding representation in an XML document is <A AttributeA="a"/> "AttributeA" may have one of the values, "a" or "b." The <xs:restriction/> element in a <xs:simpleType/> may be used to constrain the value space of data types derived from the string data type with the <xs:whitespace/> element. The whiteSpace element is represented as <xs:whiteSpace value="preserve|collapse|replace"/>. The value of the attribute "value" of element <xs:whitespace/> may be "preserve," "collapse," or "replace." If the value of an attribute "value" is "preserve," the whitepaces in the string data type are not changed. If the value of the attribue "value" is "replace," all occurrences of #x9(tab), #xA(linefeed), and #xD(carriage return) are replaced with a #x20(space). An element with a whiteSpace restriction on its string value is represented in a schema as <xs:element name="A"> If the value of attribute "value" is "replace," the string in "A" in an XML document represented as <A> Trim would be parsed as "Trim String White Space." If the value of the attribute '"value" is "collapse," the leading and trailing #x20(space) are removed, and contiguous occurances of #x20(space) are merged into a single #x20(space). If the value of the attribute "value" is "collapse," the string in "A" in an XML document represented as <A> Trim String WhiteSpace </A> would be parsed as "Trim String White Space". <xs:restriction/> element <xs:pattern/> is used to constrain the value space of data types to literals which match a regular expression. An example of a simpleType with pattern restriction would be <xs:element name="A"> An example of a corresponding representation in a XML document is <A>25-567</A> SimpleType with List Element <xs:list/> element. An element with a list of values would be <A>1.0 2 3</A> SimpleType with Union Element <xs:element name="A"> An element with a union of simpleTypes in an XML document would be <A>15</A> or <A>element A</A> "A" may be either an integer or a string. ComplexType Declaration A complexType declaration may occur in the <xs:schema/> declaration or a <xs:element/> declaration. If a complexType is declared in the <xs:schema/> element, the complexType may be referred to in the ?type? attribute of an element declaration. <xs:schema> A text(PCDATA) element is represented with a complexType as
<xsd:element name="A">
<xsd:complexType mixed="true">
<xsd:complexContent>
<xsd:restriction
base="xsd:anyType">
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
The corresponding representation of a text element in an XML document is <A>Text Element</A> An empty element is represented with a complexType as
<xsd:element name="A">
<xsd:complexType mixed="false">
<xsd:complexContent>
<xsd:restriction
base="xsd:anyType">
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
The corresponding representation of an empty element in an XML document is <A/>. An unordered element set is represented with a complexType element as
<xs:element name="A" >
<xs:complexType>
<xs:all>
<xs:element name="B"
type="xs:string"/>
<xs:element name="C"
type="xs:string"/>
<xs:element name="D"
type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
A corresponding representation of an unordered element set in an XML document is <A> An element with a choice and a sequence is represented with a complex Type as
<xs:element name="A" >
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element name="B"
type="xs:string"/>
<xs:element name="C"
type="xs:string"/>
</xs:choice>
<xs:element name="D"
type="xs:string"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
</xs:element>
A corresponding representation of an element with a choice and sequence in a XML document is <A> ComplexType with Mixed Content
<xs:element name="A" >
<xs:complexType mixed="true">
<xs:element name="B"
type="xs:string"/>
</xs:complexType>
</xs:element>
The corresponding representation of a mixed content element in an XML document is <A>Element Text ComplexType with SimpleContent Element
<xs:element name="A">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="attr"
type="xs:integer"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</element>
The corresponding representation of a simpleContent element in a XML document is <A attr="15">simpleContent Type Element</A> The simpleContent element may also be used with the <xs:restriction/> element.
<xs:element name="A">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="xs:string">
<xs:minLength value="5"/>
<xs:attribute name="a"
type="xs:integer"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</element>
The corresponding representation of a simpleContent element with a restriction in a XML document is <A a="15">Element A</A> ComplexType with ComplexContent Element <xs:element name="A"> The corresponding representation of element ?A? with complexContent in a XML document is <A><B>Element B</B><C>25</C></A> The complexContent element may be used to extend another complexType. <xs:complexType name="complexTypeA"> The corresponding representation of a complexContent element with an extension on another complexType is <A> Conclusion Resources: XML JOURNAL LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING XML NEWS
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||