Sunday, March 13, 2011

A look at XML, DTD/XSD, XSLT

Overview

XML is used all over the web. It is used as a means to transport data, best described by W3 schools as "a software and hardware independent tool for carrying information". The aim of XML is to simplify the interactions with the data and facilitate the transport of data across multiple platforms.

I have encountered XML previously while dealing with some databases and Web applications but have never actually started an XML file from scratch and neither had I ever created a DTD for my data but just made use of an existing one. I also continuously make use of specific XML created languages; particularly RSS feeds to keep myself updated on current events.

The basic structure of XML is not that complicated to understand. XML required a root element to be defined and all the other elements to be defined within it. Understanding attributes and how to properly define them was the next step followed by how to provide a structure for your XML, using a DTD.

Weekly Exercise

This week's exercise was as follows:

Create an XML file for to keep the following data about a student project: student name, student ID, project title, project category, abstract, date submitted. Try to use both elements and attributes to describe the data. Validate the XML using the W3Schools XML Validator. Create a DTD Schema for the XML file and validate the XML against the schema using the W3Schools DTD Validator.

I decided to start with the DTD instead of the XML as it seems to make more sense to start by defining the structure of your file first off, and then making use of it. I identified Student and Project as our main elements and the rest of the fields as their attributes.

The DTD created is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE studentProject [
<!ELEMENT studentProject (student, project)>
<!ELEMENT student (#PCDATA)>
<!ELEMENT project (#PCDATA)>

<!ATTLIST student name CDATA #REQUIRED>
<!ATTLIST student id CDATA #REQUIRED>
<!ATTLIST project title CDATA #REQUIRED>
<!ATTLIST project category CDATA #REQUIRED>
<!ATTLIST project abstract CDATA #REQUIRED>
<!ATTLIST project submitDate CDATA #REQUIRED>
]>

I added some sample data and tried to validate it against the W3 Schools validator but after some time we discovered that that same validator only worked on Internet Explorer. So I quickly searched for another one and found www.xmlvalidator.com.

Adding one studentProject gave no errors but when trying to add multiple projects was not possible using this DTD since the root element is studentProject itself. So I added another element called databaseSystem which incorporates a studentProject element. I thought this was it but still couldn't add multiple projects. After some research I discovered that one must add a "+" after an element that can be repeated. So the first few lines of the DTD were changed to:

<!DOCTYPE databaseSystem [
<!ELEMENT databaseSystem (studentProject+)>
<!ELEMENT studentProject (student, project)>

Adding more and more data gave no more errors in the XML Validator.

No comments:

Post a Comment