XML simple element contains only text not even any other elements or attributes.But the text can be of many different types. It can be among the types included in the XML Schema definition (boolean, string, date, etc.), or it may be a custom type that a user is free to define. Even. restrictions (facets) can be added to a data type in order to limit its content.
Defining a Simple Element
The syntax for a simple element is:
<xs:element name="aaa" type="bbb"/> |
XML Schema has a lot of built-in data types. The most common types are:
Example:
Few of XML elements:
<name>Rahul</name> <age>15</age> <currentdate>2007-05-15</currentdate> |
The corresponding simple element definitions:
<xs:element name="name" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="currentdate" type="xs:date"/> |
Simple elements may have a specified default value OR a fixed specified value .A default value is automatically assigned to the element when no other value is specified for example to set the "orange" default value .
<xs:element name="fruit" type="xs:string" default="orange"/> |
Fixed Values for Simple Elements
A fixed value is also automatically assigned to the element, and it cannot further specify another value.
In the following example the fixed value is "apple":
<xs:element name="fruit" type="xs:string" fixed="apple"/> |
A complex element contains other elements or attributes.
What is a Complex Element?
It is an XML element that contains other elements and/or attributes. They are of four types:
Note: Each of these elements may contain attributes as well!
Examples of Complex Elements
A complex empty XML element, "employee"
<employee eid="1234"/> |
A complex XML element, "employee", which contains only other elements:
<employee> <firstname>Amit</firstname> <lastname>Gupta</lastname> </employee> |
A complex XML element, "employee", which contains only text:
<employee type="category">Programmer</employee> |
A complex XML element, "event", which contains both elements and text:
<event> It occured on <date lang="norwegian">15.05.07</date> .... </event> |
Look at this complex XML element, "employee", which contains only other elements:
<employee> <firstname>Amit</firstname> <lastname>Gupta</lastname> </employee> |
We can define a complex element in an XML Schema in two different ways:
1. "employee" element can be declared directly by naming the element, like this:
<xs:element name="employee">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
|
In the above described method only "employee" element can use the specified complex type. Note that the child elements, "firstname" and "lastname", are surrounded by the <sequence> indicator. This means that the child elements must appear in the same order as they are declared.
2. "employee" element can have a type attribute refering to the name of the complex type to use:
<xs:element name="employee" type="personinfo"/> <xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
|
Using the method described above, several elements can refer to the same complex type, like this:
<xs:element name="employee" type="personinfo"/> <xs:element name="employer" type="personinfo"/> <xs:element name="teammember" type="personinfo"/> <xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
|
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: XSD Simple Elements
Post your Comment