XSD Simple Elements

In this section, you will learn about XSD simple elements. XML Schemas define the elements of XML files.


 

Ads

Tutorials   
JAXB Create XML File And Get Data From XML XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials The Simple API for XML (SAX) APIs Part I XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials XML Tutorial Ignoring Comments While Parsing an XML File Accessing XML file from Java Preventing Expansion of Entity References Getting next Tag in the XML File Replacing a Node with a New One Reading XML Data from a Stream Reading an XML document using JDOM Query XML with an XPath expression Normalize All of the Text in a Document Modifying Text by Replacement Insert a Processing Instruction and a Comment Node Getting text values from a NodeList Finding an ID Value and Print the Element Text Edit Text by Insertion and Replacement Duplicating a Subtree Deleting Two Attributes Delete the First Child of the Root Node Creating XML Tree Creating a DocumentFragment Subtree and Appending it to the Document Creates element node, attribute node, comment node, processing instruction and a CDATA section Creates a New DOM Parse Tree Replacing a Text Node with a New CDATA Section Node Create an Empty DOM Document Adding an Attribute in DOM Document Transforming XML with XSLT Transforming an XML File with XSL Transforming XML with SAXFilters Storing properties in XML file Testing EntityReferences in Xml Splitting One Text Node into Three Reading XML from a File Modifying Text by Cutting and Pasting Locating a Node by Using Siblings Locate a Node and Change Its Content Loading properties from a XML file Listing nodes used in a document Inserting a New Entry in a List Creating an InlineStreamFilter Ignoring Comments While Parsing an XML File Handling Errors While Parsing an XML File Emitting DOCTYPE Declaration while writing XML File Converting CDATA Nodes into Text Nodes Java DOM Tutorial Creating Blank DOM Document
Ads

XSD Simple Elements

     

XML Schemas define the elements of  XML files.

 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"/>

where aaa is the name of the element and bbb is the data type of the element.

XML Schema has a lot of built-in data types. The most common types are:

  • xs:string
  • xs:decimal
  • xs:integer
  • xs:boolean
  • xs:date
  • xs:time

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"/> 

Default  Values for Simple Elements

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"/>

XSD Complex Elements:

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:

  • empty elements
  • elements that contain only other elements
  • elements that contain only text
  • elements that contain both other elements and text

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>

Defining a Complex Element:

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>

     

Advertisements

Ads
Share on Google+Share on Google+

XSD Simple Elements

Posted on: May 25, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: XSD Simple Elements  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads