What is XSD? - Examples & Tutorial

Instructor: Lonny Meinecke

Lonny teaches psychology classes at King University, and has a bachelor's degree in IT and a doctorate in psychology.

This lesson is about XSD, which is frequently used with XML. This lesson will define XSD, show some examples, and present a brief tutorial on how to help validate XML files using XSD.

What Is XSD?

The first thing you will want to know is what XML is. That's because XSD is all about XML. So to begin, you should know that XML is a way to put just about any kind of information into a web document so that other users can get to that information without knowing much about it. This is great, but over time, it has become obvious that XML has so few rules that accidents can happen. Folks may even misinterpret your information. That's where XSD comes in. XSD is a kind of quality control (QC) language. With XSD, you can make sure that information in an XML file is described well and has some way to be validated.

Officially, XSD stands for XML Schema Definition. What is a schema? A schema is simply a standard way to structure information. Think of it as a sort of schematic for words and ideas so they get put in the right place and are understood a certain way.

XML Without XSD

Let's see an example so you get a visual idea. The following example is a simple XML file that contains some information about the three brightest stars at night.

brightstar xml file

As you can see, we want to share the name, magnitude, and distance of these stars. However, each of these pieces of information is rather special -- the first one is text, the second one is a decimal, and the third one is an integer. It is very possible that users we share our list of stars with will misinterpret one of these (for example, mistake an integer for a decimal or vice versa). This may not seem all that important, but sometimes minor things can create problems that are hard to track down. So, it's much better to make sure someone in quality control is watching over our data. This next example will do that for us.

Now For Some XSD

XSD files are made up of tags, just like an XML file. In fact, XSD files actually are XML files. Isn't that handy? This was not always the case. Like an XML file, an XSD file has elements, and each element must have a name and a type. There are simple, complex, and custom (user-defined) types. A simple element looks like this:

sample XSD element

As you can see, all we need is a name for the element (to distinguish it) and a type to indicate what kind of element we can expect. Now when a user accesses the 'magnitude' piece of data, it will always be checked to make sure it is a decimal (not a string or an integer). There are many predefined types, such as string, integer, and decimal.

We could just define our brightstar schema with all simple elements, since all we need are predefined types. Here is an example:

3 separate elements

This is not very versatile, however. It would be much better to use a complex type so we ensure all of the attributes stay together. This is how we do that:

complex type example

The 'sequence' element makes sure our attributes for brightstar stay in order, as well as staying together under a complex type. We can even make each attribute of brightstar separately accessible with a bit more work, perhaps to search for stars of a certain absolute magnitude. But for this example, we do not need that capability.

XML With Help From XSD

Now we will look at the full XSD file that goes along with our XML file. It will make sure each of our pieces of information about brightstar data is understood correctly.

xsd schema file

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account