Click here to Skip to main content
15,895,370 members
Articles / Programming Languages / XML
Article

Xml schema defines the structure of an xml document

Rate me:
Please Sign up or sign in to vote.
2.11/5 (5 votes)
14 Sep 20076 min read 23.3K   7   2
Xml schema defines the structure of an xml document

Introduction

we see XML having larger significance with Web Services being the next big thing. And all Web Service platforms which are built tightly around XML as a technology.

Background

While the basic websites will still be raised on a solid foundation of HTML, e-commerce sites will have solid XML reinforcement layers. Even Microsoft believes that XML is the way the web is heading for web/database integration. And Sun and the Linux community have been there supporting XML for long. Future versions of all Microsoft products will include native XML support. And .Net is build around XML.

1. Flexible tag markup.The most important feature of XML is that the elements of your document can be whatever you choose. This flexibility allows you to markup your documents using easily understood elements—with meaning rather than presentation. This in turn yields a large return on investment, with limitless possibilities for expressing information due to an infinite tag set. Marking up documents with XML produces highly reusable text—written once and available for use in many different contexts.
2. XML lets you define document structures and make the inherent structure explicit. The flexibility described above allows you to define structure according to your needs. All documents have some implicit structure, be it as simple as a paragraph formatting.With XML, you can take the implicit and make it explicit, making it possible toprocess the text based on its true structure.
3. XML is vendor-independent. Your company is not locked into using a particularvendor, which renders XML documents timeless. This benefit is especially important if you are legally required to keep your documents for a long period of time. In such cases, tying your documents to one vendor proves risky, since even the most establishedvendors may not exist in a few years. One of the most common mistakes made in the business world, aside from looking for a quick fix to a complex problem, is not adapting to change. XML provides you with guarantees by allowing you to design your own standards for re-use.
4. XML is application independent. Just as documents are not tied to a vendor, likewise,they are not locked to an application. Therefore, your documents remain available even when the software becomes obsolete. Additionally, you are afforded the freedom to change the data formats by simply applying a new stylesheet. Using XML to create your documents ensures that your data remains future-proof and platform-independent.
5. XML documents can be validated. In many situations, validation is crucial. Data must be verified to ensure your information will be correctly processed by other applications. Validation is an essential part of successful data management.
6. XML provides context. Using the structures defined by a DTD, it is possible forusers to hone in on relevant information quickly. XML can be used to describe datacontained in a wide variety of applications, from describing collections of web pages to data records. There is no need for a built-in description of the data, since the data is self-describing.
7. XML supports information re-use. The rich structure and device independence of XML means that it can be easily re-used in many different contexts. Unlike typical data formats, XML also makes it possible to re-use information at a very fine grained-level:(i.e. the level of a single element). For instance, XML's structure makes it easy to retrieve all the section titles used on a website.

Using the code

XML SCHEMA

Xml schema is an alternative xml-way to DTD.

Xml schema defines the structure of an xml document i.e what are the legal blocks present in xml document.

Xml schema language is also known as xml schema definition(XSD).

Eg.

Note.xml

<?xml version="1.0">

<note>

<to>Mangesh</to>

<from>Xml</from>

<body>Hi to all?</body>

</note>

A Schema File: called note.xsd

<?xml version="1.0">

<xs:schema xmlns:xs =" http://www.w3.org/2001/xmlschema"

targetNamespace = "http://www.w3schools.com"

xmlns = "http://www.w3schools.com"

elementFormDefault = "qualified">

<xs:element name="note">

<xs:complextype>

<xs:sequence>

<xs:element name="to" type="xs:string"/>

<xs:element name="from" type="xs:string"/>

<xs:element name= "bode" type="xs:string"/>

</xs:sequence>

</xs:complextype>

</xs:element>

</xs:schema>

In above note element is of type complex since it having another element while other

Elements are simple since they do not having other elements.

What is a Simple Element?

A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes.

However, the "only text" restriction is quite misleading. The text can be of many different types. It can be one of the types included in the XML Schema definition (boolean, string, date, etc.), or it can be a custom type that you can define yourself.

You can also add restrictions (facets) to a data type in order to limit its content, or you can require the data to match a specific pattern.


Defining a Simple Element

The syntax for defining a simple element is:

<xs:element name="xxx" type="yyy"/>


where xxx is the name of the element and yyy 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

Here are some XML elements:

<lastname>Refsnes</lastname>


<age>36</age>


<dateborn>1970-03-27</dateborn>

And here are the corresponding simple element definitions:

<xs:element name="lastname" type="xs:string"/>


<xs:element name="age" type="xs:integer"/>


<xs:element name="dateborn" type="xs:date"/> 


Default and Fixed Values for Simple Elements

Simple elements may have a default value OR a fixed value specified.

A default value is automatically assigned to the element when no other value is specified.

In the following example the default value is "red":

<xs:element name="color" type="xs:string" default="red"/>

A fixed value is also automatically assigned to the element, and you cannot specify another value.

In the following example the fixed value is "red":

<xs:element name="color" type="xs:string" fixed="red"/>

What is an attribute?

An simple element can not have an attribute if element having attribute consider as

An complex element.

Declaration:

<xs:attribute name="name" value="xs:string">

Type may be

Xs:string

Xs:integer

Xs:decimal

Xs:Boolean

Xs:date

Xs:time

An attributes may have default or fixed values

<xs:attribute name="lang" type="xs:string" default="English">

Fixed:

<xs:attribute name="lang" type="xs:string" fixed="English">

Optional and Required attribute:

Attributes are optional bydefault.

You can specify this required using use keyword

<xs:attribute name="lang" type="xs:string" use="required">

You can add your own restrictions on xml element called facets you can say that validation for an attribute or allowed values.

Eg

<xs:element name="gender">

<xs:simpletype>

<xs:restriction base="xs:string">

<xs:pattern value="male|female" />

</xs:restriction>

</xs:element>

Points of Interest

Points of Interest

Users can easily handle structured databases on the web. XML can be easily changed into database files and vice-versa. So, users can directly import a variety of data on the web to their own database.

What makes XML such a great technology? Why is XML so popular among all leading vendors as far as their future technology options are concerned? Is XML all style and no substance? What are the new business opportunities that open up with XML? We take a look at all these issues….

Compared to HTML, XML has a number of advantages of XML as listed in

HTML

XML

A word processor entwines content

Concept of structure is hardly present.

The trouble with WYSIWYG is what yousee is all you get

HTML documents cannot be processed

Structure of document is as important as the content itself.

Presentation of information is also important, but is kept well separated from the content.

In XML, you create document content by concentrating on how information is arranged

XML documents are essentially databases of information. They can be processed, harvested, reported on and queried just like traditional databases.

History

.............................

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
i will give it later on......

Comments and Discussions

 
QuestionReferences? Pin
Georgi Atanasov14-Sep-07 9:15
Georgi Atanasov14-Sep-07 9:15 
Okay,

It seems to me that you are using some resources like books, technical materials, white papers, etc. - could you point us to some of them? By the way does copying of materials make an article on CP???



Thanks,
Georgi

AnswerRe: References? [modified] Pin
Mangesh.v.s16-Sep-07 19:17
Mangesh.v.s16-Sep-07 19:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.