XML Schema Definition Part 3


Namespace

We will start with creating an XSD file using Eclipse. A schema definition file is an XML file. After it is created in the IDE we can create a namespace for our project. We will use a very simple example of a list of books.

In Eclipse, you first create a new project. Everything in Eclipse is a project. We will call our project BooksXML. We will call our schema file Books.xsd and we will call our xml file Books.xml. We will create a prefix called bks and use this website to uniquely identify the namespace. When you first cerate a new xsd file in a new project in Eclipse you get the following code. We will modify that code to create a namespace. The targetnamespace below is used to define the elements for our elements that we are defining.

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://www.example.org/Books" 
xmlns:tns="http://www.example.org/Books" elementFormDefault="qualified">
</schema>

In our simple example, and it’s always good to master the simple things before getting complex, we will modify our code to include a prefix and we will begin to create a schema for Books. First, we must define our root element for our XML document. We use element for the XML namespace defined at http://www.w3.org/2001/XMLSchema. We use a complex type to list the elemnets in our file. We use either sequence, all or choice. Also we must use our complex type in the root otherwise it will not get used, it will be ignored. Our code now becomes as follows.

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://porterwebsites.com/Book"
	xmlns:bks="http://porterwebsites.com/Book" elementFormDefault="qualified">

	<element name="Book" type="bks:Book" />

	<complexType name="Book">
		<sequence>
			<element name="Title" type="string" />
			<element name="Author" type="string" />
		</sequence>
	</complexType>
</schema>

  • create a targetnamespace and a prefix
  • create the root element
  • create a complex type (sequence: means all are required and in that order) and elements
  • set the type of the root element to your complex type: type=”bks:Book

Create an XML Based on XSD

In Eclipse, it is easy to create an XML from an XSD. File, New, Other…, XML File (under the list of templates), Next, select the project, give it a name (Books.xml), Next and select Create XML file from an XML schema file, Next, select Books.xsd under your project (click the arrow first), Next and lastly click Finish. Now you have what you see below.

<?xml version="1.0" encoding="UTF-8"?>
<bks:Book xmlns:bks="http://porterwebsites.com/Book" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://porterwebsites.com/Book Books.xsd ">
  <bks:Title>My Fabulous XSD Book</bks:Title>
  <bks:Author>John Johnson</bks:Author>
</bks:Book>

In the next post we will create an XSD that allows us to have multiple elements in our XML file. We will change the example to People.

Series Navigation<< XML Schema Definition Part 2XML Schema Definition Part 4 >>

Leave a comment

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