Eclipse has support for XML Schema so you can develop and test your own schemas for free!

If you are new to XML Schema, the XML Schema tutorial from www.w3schools.com is a good starter to enter the world of XML Schema.


Make sure you installed the necessary Web Tools Platform plug-ins so you have the necessary features to develop your schemas.

At the moment I’m using Eclipse version 3.7.1.

In Eclipse, create a new general project, in that project create a new XML Schema file. I called the file MySchema.xsd.

Ecplise starts with a nice template.

It makes already a target namespace for you, in this case http://www.example.org/MySchema.
The target namespace is the unique namespace in which you declare your elements.
The purpose of a namespace is to prevent collisions, so if you declare an element bank (furniture) with a different content model as someone else, e.g. bank (financial institution), these elements can live together if they are in a different namespace. I will demonstrate such an example in a future article.

The target namespace here gets the prefix tns:.
The default namespace is used here for the XML Schema itself (referred with xmlns= without a prefix), so the elements in this namespace don’t have a prefix.

You also could have written:

  1.  
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  4.  targetNamespace="http://www.example.org/MySchema"
  5.  xmlns:tns="http://www.example.org/MySchema" elementFormDefault="qualified">
  6. </xs:schema>

The schema element is in the http://www.w3.org/2001/XMLSchema namespace and referred with the xs: prefix. This prefix is just an alias for the full http://www.w3.org/2001/XMLSchema namespace. So you can use whatever prefix you want.

So the schema element has to be in the namespace http://www.w3.org/2001/XMLSchema.

If you write:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  3.  targetNamespace="http://www.example.org/MySchema"
  4.  xmlns:tns="http://www.example.org/MySchema" elementFormDefault="qualified">
  5. </schema>

you will get an error while validating the schema (right click the filename and select “validate”):

  1. The namespace of element 'schema' must be from the schema namespace,
  2. 'http://www.w3.org/2001/XMLSchema'.

Until now we don’t have declared any elements in our schema.
Let’s add one element to the schema:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <schema xmlns="http://www.w3.org/2001/XMLSchema"
  3.  targetNamespace="http://www.example.org/MySchema"
  4.  xmlns:tns="http://www.example.org/MySchema" elementFormDefault="qualified">
  5.  <element name="myRootElement"/>
  6. </schema>

Now let’s create an XML file based on this very basic schema.

And here we have the result:

We have the element myRootElement in the namespace http://www.example.org/MySchema referred to it with the prefix tns.

We refer to our schema with the attribute xsi:schemaLocation, the first value of the attribute is the target namespace, the second value is the filename location.

As you can see the attribute schemaLocation lives in its own namespace http://www.w3.org/2001/XMLSchema-instance and is not part of the schema we made.

If we use as the default namespace the target namespace we can write:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <myRootElement
  3.  xmlns="http://www.example.org/MySchema"
  4.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.  xsi:schemaLocation="http://www.example.org/MySchema MySchema.xsd ">
  6. </myRootElement>
Rating 2.40 out of 5
[?]