How to Make XSD
The W3C, or World Wide Web Consortium, has recommended the XML Schema language (XSD) as a replacement to the older Document Type Definition language for defining the structure of XML databases and documents. XSD's syntax has a number of enhancements over DTD, including a more XML-like syntax and the standardization of many commonly used data types.
Instructions
-
-
1
Decide on the structure of the data you wish to define. You may wish to perform this step on a sheet of paper by either making an outline or drawing a graph. This tutorial will describe data for a company's employees with the following outline:
Department
--->Employee
------>First Name
------>Last NameWe see here that the company has departments, which are divided up into employees, which in turn have first and last names. A real employee database would contain much more information than this, but this is enough to illustrate the example.
-
2
Open a new text file. Save the file with an XSD extension. If you use Windows and are using Notepad as opposed to a dedicated programming text editor, this may require turning on "Show File Extensions" in the "Folder Options" settings of "My Computer."
-
-
3
Type the following into your document:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://w3.org/2001/XMLSchema"></xs:schema>
The first line is the header that identifies this document as an XML document, and the rest are tags to enclose the full document. All code to follow will have to go between the lines <xs:schema....> and </xs:schema>. This is a common feature of the XML language: tags are opened with a command and closed with the same command preceded by a forward slash.
-
4
Define simple elements. A simple element is a piece of information that does not include other pieces of information within it. In the employee outline example given above, the simple elements are First Name and Last Name.
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/> -
5
Define complex elements and establish references. Notice, the Employee element within Department is not a simple element, because it contains the element's first and last name. Employee and Department are complex elements.
<xs:element name="employee">
<xs:complexType>
<xs:element ref="firstName"/>
<xs:element ref="lastName"/>
</xs:complexType>
</xs:element>Notice that the xml parameter "ref=" refers to the simple elements defined above. Do the same for Department to indicate that it contains employees.
<xs:element name="department">
<xs:complexType>
<xs:element ref="employee"/>
</xs:complexType>
</xs:element>
-
1
Tips & Warnings
Use an XML Editor. While it is entirely possible to write XML and XML Schema documents in a standard text editor, a dedicated XML editor or programming text editor will include advanced features to assist in using correct and standards-compliant syntax.