Browse By

How to create XML Schema

My notes of XML schema from Methods of Software Development course (CS6520) in Northeastern University.

#1. Basic question, what is XML schema?

Again, we can lookup the formal definition in the W3C website and Wikipedia, but we all know that these formal definition doesn’t help a beginner like us. So, according to djitz, the simple definition of XML schema is:

A document (XML) that defines XML structures we want to make.

Sounds too casual, well, who cares right? As long as we understand. 😀

#2. I don’t really understand yet, gimme examples!

Of course we all need examples, that’s how we get better understanding of what we are talking about.

Let’s consider this situation. You want to make an XML for your library and you created something like this: (*This is called an XML instance)

[xml]

How to get Rich
Mr. Rich Guy 39.99
[/xml]

Ok, pretty simple right? Now, suppose your friend think that the XML structure you created is such a brilliant idea and she wants to create the same XML structure, but for some reason you cannot give her your XML, you need to describe it. How can you do that?

That’s the time you want to create an XML schema.

The XML schema for the library XML above will look like this:

[xml]













[/xml]

I hope you don’t get confused yet, try to compare the schema with our previous XML example and you will see how it all makes sense.

#3. Is there no easy way to do this?

Of course there is, some of the XML editor have the features called ‘Generate Schema’, so given an XML instance (such as our book XML), it will generate the schema.

A word of caution, as always the case with all auto-generated codes, it’s usually more verbose.

I use Oxygen XML Editor, and to generate the XML schema from XML instance, I go to Tools>Generate/Convert Schema.

Here’s the result:

[xml]






















[/xml]

#4. How to know that my XML schema is correct?

Good question, always check whether your XML schema is correct (if you start from XML instance) by validating the XML instance with your schema.

In Oxygen, I simply go to Document > Validate > Validate with, and choose the XML schema I created.

#5. Which one do we need to create first, XML instance or XML schema?

Another good question! Since we are learning how to create XML schema here, so I showed it by starting from XML instance. But suppose we understand the concept of XML schema already, then we can start from creating XML schema.

Links:
W3CSchool


2 thoughts on “How to create XML Schema”

Leave a Reply

Your email address will not be published. Required fields are marked *