XSD (XML Schema Definition) and XML Documents
The purpose of this assignment was to:
- Learn what XSD is.
- Learn how and why to put constrains on a XML document by the use of XSD.
- Understand why XSD is a better choice than DTD.
The description and my solution of the XSD(XML Schema Defintion) - assignment can be viewed at the bottom of this page.
What is an XML Schema Definition?
The purpose of an XML schema is to define the structure of an XML document. It does this by defining the allowed components of a document, just like a DTD does.
An XML Schema is sometimes referred to as XSD, which stands for XML Schema Definition.
XML Schema Definition Language is a language written in XML.
An XML Schema is used for defining:
- data types for elements and attributes
- elements that are allowed in a XML document
- attributes that are allowed in a XML document
- elements that are child elements
- the order in which child elements can appear in an XML document
- the number of allowed child elements
- whether elements are have empty content or text content
- fixed and default values for elements and attributes
Benefits of XML Schemas
- XML Schemas have support for Data Types
- Since XML Schemas written in XML they are extensible
- XML Schemas have support for Namespaces
XML Schemas Support Data Types
XML Schemas support for data types makes it:
- easier to describe allowed document content
- easier to validate data
- easier to work with database data
- easier to define restrictions on data
- easier to define data formats
- easier to convert data between different data types
XML Schemas are XML-based
Since XML Schemas are XML-based:
- You don't have to learn a new language
- You can use an XML parser to parse and validate XML Schema files
- You can manipulate your XML Schema with the XML Document Object Model(DOM)
- You can use XSLT to transform your Schema
XML Example Document and Example Schema
This is a simple XML document implementing a "message":
<?xml version="1.0" encoding="ISO-8859-1"?> <message> <receiver >Buck</receiver> <sender>Lenny</sender> <subject>Welcome</subject> <content>Welcome Buck!</content> </message>
and this is an XML Schema to describe it's structure:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.webpelican.com" xmlns="http://www.webpelican.com" elementFormDefault="qualified"> <xs:element name="message"> <xs:complexType> <xs:sequence> <xs:element name="receiver" type="xs:string"/> <xs:element name="sender" type="xs:string"/> <xs:element name="subject" type="xs:string"/> <xs:element name="content" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Because the message element contains child elements, it's is a complex type. The child elements receiver, sender, subject and content are simple types since they don't have other elements as child elements.
The Reference to the XML Schema
This is the message document now with a connection to (reference to) the XML Schema above:
<?xml version="1.0"?> <message xmlns="http://www.webpelican.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.webpelican.com message.xsd"> <receiver>Lenny</receiver> <sender>Bobba</sender> <subject>I like to hate XML Schemas</subject> <content>I've grown to like hating XML Schemas!</content> </message>
Assignment Description
Put constrain on the cv-template from XML Basics by using an XML Schema.
My solution, Assignment Files
-
cv-template.xml
Now with a reference to an XML Schema that define the allowed elements and attributes. -
cv-template.xsd
This is the XML Schema which is referenced to in the XML document.
