Saturday, 26 November 2011

CMT3315 - Lab 04 - XML Syntax

Quick Questions

1. What does XML stand for? And CSS?

XML stands for Extensible Markup Language. CSS stands for Cascading Style Sheets.

2. Is this XML line well-formed? Say why.
<b><i>This text is bold and italid</b></i>

No this XML is not well formed, the </b></i> closing tags need to be inverted to reflect the order they were opened.

3. Is this XML document well-formed? Say why.

<? xml version="1.0" ?>
<greeting>
     Hello, World!
</greeting>
<greeting>
     Hello Mars too!
</greeting>

No this XML is not well formed, a well formed XML document should only have one root element.

Longer Questions

1. Write an XML document that contains the following information: the name of this course, the name if this building, the name of this room and the start and end times of this session. Choose appropriate tags. Use attributed for the start and end times.

Answer

<?xml version="1.0" ?>
<sessions>
     <session start="2011-11-22T18:00:00" end="2011-11-22T21:00:00">
          <name>CMT3315</name>
          <building>A</building>
          <room>4<room>
     </session>
</ sessions >

2. Have a look at the XML document below, Identify all the syntax errors.

Answer

I have highlighted all lines in the XML document in RED, including a GREEN line describing what is wrong. I have also included a corrected valid XML version of this document at the end.

<?xml version= "1.0" ?>
<!DOCTYPE bookStock SYSTEM "bookstock.dtd">
<bookstore>
No corresponding </bookstore> close tag
     <book category="Cooking">
          <title lang="en">Everyday Italian</title>
          <author>Giada De Laurentiis</author>
          <1stEdition>2005</1stEdition >
          <2ndEdition>2007</2ndEdition >
          Tags may not start with numbers
          <price>19.99</price currency="pounds sterling">
          Closing tags may not contain attributes
     </book>
     <book category="Children’>
     Opening double quotes must be matched by closing double quotes
          <title lang="en">Harry Potter and the enormous pile of money</title>
          <!—best selling children’s book of the year --2009 -->
          <author>J K. Rowling</author>
          <1stEdition>2005</1stEdition>
          Tags may not start with numbers
          <price>29.99</Price>
          Opening <price> tag does not match closing </Price tag> - XML is case sensitive
     </book>
     <book category="Web">
          <title lang="en">Learning XML</title>
          <author>Erik T. Ray</author>
          <1stEdition>2003</1stEdition>
          <2ndEdition >2008</2ndEdition >
          Tags may not start with numbers
          <price>29.95</discount>
          <discount>15%</price>
          <price> tag must be closed with </price> tag, <discount> tag must be closed
           with </discount>
     </book>
     <book category="Computing">
         <title lang=en>
          Insanely great – the life and times of Macintosh, the computer that changed everything
          </title>
          Attribute values must be enclosed in either single or double quotes
          <author <!—other authors not listed -->>Steven Levy</author>
          <1stEdition>1994</1stEdition>
          Tags may not start with numbers
         <price>9.95</discount>
         <discount>15%</price>
         <price> tag should be closed with </price>, <discount> should be closed 
         with </discount>
     </book>

Corrected XML document


<?xml version= "1.0" ?>
<!DOCTYPE bookStock SYSTEM "bookstock.dtd">
<bookstore>
    <book category="Cooking">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <firstEdition>2005</firstEdition>
        <secondEdition>2007</secondEdition>
        <price currency="GBP">19.99</price >
    </book>
    <book category="Children">
        <title lang="en">Harry Potter and the enormous pile of money</title>
        <author>J K. Rowling</author>
   <1stEdition>2005</1stEdition>
        <price>29.99</price>
    </book>
    <book category="Web">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <firstEdition>2003</firstEdition>
        <secondEdition>2008</secondEdition>
        <price>29.95</price>
        <discount>15%</discount>
    </book>
    <book category="Computing">
        <title lang="en">
        Insanely great – the life and times of Macintosh, the computer that changed everything
        </title>
        <author>Steven Levy</author>
        <firstEdition>1994</firstEdition>
        <price>9.95</price>
        <discount>15%</discount>
    </book>
</bookstore>

3. You are asked to produce a Document Type Declaration for a class of XML documents called "memo". You come up with this .dtd file.

Your client says "That's all very well", but every memo has to have a date, and some of them have to have a security classification too (You might want to write "Secret" at the top). And a memo has a serial number - I think that's what you'd call an attribute, isn't it?". How would you amend this .dtd so that it did what the client wanted?

Answer


<!DOCTYPE memo
[
<!ELEMENT memo (date,to,from,heading,body,classification)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
<!ELEMENT classification (#PCDATA)>
<!ATTLIST memo serialNumber ID #REQUIRED>
]>



Saturday, 19 November 2011

CMT3315 - Lab 03 - XML Syntax

Question 1

Write an XML document that contains the following information: your name, your email address, your student number, your home town and your date of birth. Choose appropriate tags. Use attributes for the date of birth.

Answer

<?xml version="1.0" ?>
<students>
     <student dateOfBirth="26-08-1988">
          <name>Daniel Vella</name>
          <email>daniel.vella@domain.com</email>
          <studentNumber>M123456789</studentNumber>
          <town>Zabbar</town>
     </student>
</students>

Question 2

Have a look at the XML document below. Identify all the syntax errors.

Answer

I have highlighted all lines in the XML document in RED, including a GREEN line describing what is wrong. I have also included a corrected valid XML version of this document at the end.

<?xml version= “1.0” ?>
<!DOCTYPE countryCollection SYSTEM "countryList.dtd">
<CountryList>
No corresponding closing tag </CountryList>
     <Nations TotalNations ="3"/>
     No corresponding closing tag </Nations>
     <!--Data from CIA --Year Book -->
     <Country CountryCode="1">
     Closing tag </country> does not match opening tag </country> - XML is case sensitive
          <OfficialName>United States of America</officialName>
          Opening tag <OfficialName> does not match closing tag </officialName> - XML is 
          case sensitive
          <Label>Common Names:</label>
Opening tag < Label> does not match closing tag </ label > - XML is case sensitive
         <CommonName>United States</commonName>
Opening tag <CommonName> does not match closing tag </commonName> - XML is
          case sensitive
         <CommonName>U.S.</commonName>
Opening tag <CommonName> does not match closing tag </commonName> - XML is
          case sensitive
          <Label>Capital:</capital>
         <Capital cityNum="1">Washington, D.C. </label>
          Label tag must be closed with a corresponding </Label> tag not </capital>
          <Capital> tag must be closed with a corresponding </Capital> tag not </label>
          <2ndCity cityNum="2">New York </2ndCity>
Tag names must always start with an alphabet character
          <Label>Major Cities:</label>
Opening tag < Label> does not match closing tag </ label > - XML is case sensitive
         <MajorCity cityNum="3">Los Angeles </majorCity>
Opening tag < MajorCity> does not match closing tag </ majorCity> - XML is
          case sensitive
         <MajorCity cityNum="4">Chicago </majorCity>
Opening tag < MajorCity> does not match closing tag </ majorCity> - XML is
case sensitive
         <MajorCity cityNum="5'>Dallas </majorCity>
Opening tag < MajorCity> does not match closing tag </ majorCity> - XML is
case sensitive
         Opening double quotes must be closed with double quotes
         <Label>Bordering Bodies of Water:</label>
Opening tag < Label> does not match closing tag </ label > - XML is case sensitive
          <BorderingBodyOfWater> Atlantic Ocean </borderingBodyOfWater>
Opening tag < BorderingBodyOfWater> does not match closing 
tag </ borderingBodyOfWater> - XML is case sensitive
         <BorderingBodyOfWater> Pacific Ocean </borderingBodyOfWater> 
Opening tag < BorderingBodyOfWater> does not match closing
tag </ borderingBodyOfWater> - XML is case sensitive
         <BorderingBodyOfWater> Gulf of Mexico </borderingBodyOfWater>
Opening tag < BorderingBodyOfWater> does not match closing 
tag </ borderingBodyOfWater> - XML is case sensitive
          <Label>Bordering Countries:</label>
Opening tag < Label> does not match closing tag </ label > - XML is case sensitive
          <BorderingCountry CountryCode="1"> Canada </borderingCountry>
Opening tag < BorderingCountry> does not match closing
tag </ borderingCountry> - XML is case sensitive
         <BorderingCountry CountryCode ="52"> Mexico </borderingCountry>
Opening tag < BorderingCountry> does not match closing 
tag </ borderingCountry> - XML is case sensitive
     </country>
     Closing tag </country> does not match opening tag </country> - XML is case sensitive
      <Country CountryCode="81">
     Closing tag </country> does not match opening tag </country> - XML is case sensitive
         <OfficialName> Japan </officialName>
          Opening tag <OfficialName> does not match closing tag </officialName> - XML is 
          case sensitive
         <Label>Common Names:</label>
Opening tag < Label> does not match closing tag </ label > - XML is case sensitive
         <CommonName> Japan </commonName>
Opening tag <CommonName> does not match closing tag </commonName> - XML is
          case sensitive
         <Label>Capital:</label>
Opening tag < Label> does not match closing tag </ label > - XML is case sensitive
         <Capital>Tokyo</capital cityNum="1">
Opening tag < Capital> does not match closing tag </ capital > - XML is case sensitive
         attributes are not allowed in closing tags
          <2ndCity cityNum="2">Osaka </2ndCity>
Tag names must always start with an alphabet character
         <Label>Major Cities:</label>
Opening tag < Label> does not match closing tag </ label > - XML is case sensitive
         <MajorCity cityNum="3">Nagoya </majorCity>
Opening tag < MajorCity> does not match closing tag </ majorCity> - XML is
case sensitive
         <MajorCity cityNum="4">Osaka </majorCity>
Opening tag < MajorCity> does not match closing tag </ majorCity> - XML is
case sensitive
          <MajorCity cityNum="5'>Kobe </majorCity>
Opening tag < MajorCity> does not match closing tag </ majorCity> - XML is
case sensitive
         Opening double quotes must be closed with double quotes
         <Label>Bordering Bodies of Water:</label>
Opening tag < Label> does not match closing tag </ label > - XML is case sensitive
         <BorderingBodyOfWater>Sea of Japan </borderingBodyOfWater>
Opening tag < BorderingBodyOfWater> does not match closing 
tag </ borderingBodyOfWater> - XML is case sensitive
         <BorderingBodyOfWater>Pacific Ocean </borderingBodyOfWater>
Opening tag < BorderingBodyOfWater> does not match closing 
tag </ borderingBodyOfWater> - XML is case sensitive
      </country>
     Closing tag </country> does not match opening tag </country> - XML is case sensitive
     <Country CountryCode="254">
     Closing tag </country> does not match opening tag </country> - XML is case sensitive
         <OfficialName> Republic of Kenya </officialName>
          Opening tag <OfficialName> does not match closing tag </officialName> - XML is 
          case sensitive
         <Label>Common Names:</label>
Opening tag < Label> does not match closing tag </ label > - XML is case sensitive
         <CommonName> Kenya </commonName>
Opening tag <CommonName> does not match closing tag </commonName> - XML is
          case sensitive
         <Label>Capital:</label>
Opening tag < Label> does not match closing tag </ label > - XML is case sensitive
         <Capital cityNum='1'>Nairobi </capital>
Opening tag < Capital> does not match closing tag </ capital > - XML is case sensitive
         <2ndCity cityNum='2'>Mombasa</2ndCity>
Tag names must always start with an alphabet character
         <Label>Major Cities:</label>
Opening tag < Label> does not match closing tag </ label > - XML is case sensitive
         <MajorCity cityNum='3'>Mombasa </majorCity>
Opening tag < MajorCity> does not match closing tag </ majorCity> - XML is
case sensitive
         <MajorCity cityNum='4'>Lamu </majorCity>
Opening tag < MajorCity> does not match closing tag </ majorCity> - XML is
case sensitive
         <MajorCity cityNum='5'>Malindi </majorCity>
Opening tag < MajorCity> does not match closing tag </ majorCity> - XML is
case sensitive
         <MajorCity cityNum='6' cityNum='7'>Kisumu-Kericho </majorCity>
Opening tag < MajorCity> does not match closing tag </ majorCity> - XML is
case sensitive
Duplicate attributed cityNum not allowed in XML
         <Label>Bordering Bodies of Water:</label>
Opening tag < Label> does not match closing tag </ label > - XML is case sensitive
         <BorderingBodyOfWater <!--Also Lake Victoria --> > Indian Ocean
          </borderingBodyOfWater>
Opening tag < BorderingBodyOfWater> does not match closing 
tag </ borderingBodyOfWater> - XML is case sensitive
          <BorderingBodyOfWater tag not closed properly
          Comments not allowed inside <tags>
          Special characters such as < and > need to be escaped when used in XML as text
      </country>
  Closing tag </country> does not match opening tag </country> - XML is case sensitive



Corrected XML document

<?xml version= “1.0” ?>
<!DOCTYPE countryCollection SYSTEM "countryList.dtd">
<CountryList>
    <Nations TotalNations ="3">
        <Country CountryCode="1">
            <OfficialName>United States of America</OfficialName>
            <Label>Common Names:</Label>
            <CommonName>United States</CommonName>
            <CommonName>U.S.</CommonName>
            <Label>Capital:</Label>
            <Capital cityNum="1">Washington, D.C. </Capital>
            <SecondCity cityNum="2">New York </SecondCity>
            <Label>Major Cities:</Label>
            <MajorCity cityNum="3">Los Angeles </MajorCity>
            <MajorCity cityNum="4">Chicago </MajorCity>
            <MajorCity cityNum="5">Dallas </MajorCity>
            <Label>Bordering Bodies of Water:</Label>
            <BorderingBodyOfWater> Atlantic Ocean </BorderingBodyOfWater>
            <BorderingBodyOfWater> Pacific Ocean </BorderingBodyOfWater>
            <BorderingBodyOfWater> Gulf of Mexico </BorderingBodyOfWater>
            <Label>Bordering Countries:</Label>
            <BorderingCountry CountryCode="1"> Canada </BorderingCountry>
            <BorderingCountry CountryCode ="52"> Mexico </BorderingCountry>
        </Country>
        <Country CountryCode="81">
            <OfficialName> Japan </OfficialName>
            <Label>Common Names:</Label>
            <CommonName> Japan </CommonName>
            <Label>Capital:</Label>
            <Capital cityNum="1">Tokyo</Capital>
            <SecondCity cityNum="2">Osaka </SecondCity>
            <Label>Major Cities:</Label>
            <MajorCity cityNum="3">Nagoya </MajorCity>
            <MajorCity cityNum="4">Osaka </MajorCity>
            <MajorCity cityNum="5">Kobe </MajorCity>
            <Label>Bordering Bodies of Water:</Label>
            <BorderingBodyOfWater>Sea of Japan </BorderingBodyOfWater>
            <BorderingBodyOfWater>Pacific Ocean </BorderingBodyOfWater>
        </Country>
        <Country CountryCode="254">
            <OfficialName> Republic of Kenya </OfficialName>
            <Label>Common Names:</Label>
            <CommonName> Kenya </CommonName>
            <Label>Capital:</Label>
            <Capital cityNum='1'>Nairobi </Capital>
            <SecondCity cityNum='2'>Mombasa</SecondCity>
            <Label>Major Cities:</Label>
            <MajorCity cityNum='3'>Mombasa </MajorCity>
            <MajorCity cityNum='4'>Lamu </MajorCity>
            <MajorCity cityNum='5'>Malindi </MajorCity>
            <MajorCity cityNum='6'>Kisumu-Kericho </MajorCity>
            <Label>Bordering Bodies of Water:</Label>
            <BorderingBodyOfWater>Indian Ocean </BorderingBodyOfWater>
            <BorderingBodyOfWater>Lake Victoria</BorderingBodyOfWater>
        </Country>
    </Nations>
</CountryList>

Saturday, 12 November 2011

CMT3315 - Lab 02 - HTML & XML Documents

Question 1

Have a look at the documents below, some are XML documents, some are HTML documents, some may be neither. See if you can decide which are which.

Answers
Document 1

This document is an HTML document, it is constructed with some of the well known HTML tags such as <head>, <title>, <body>, <a> etc… It also includes a <script> tag example which is used to load javascript code. Most HTML tags include attributes such as <a href=” http://ads.touregypt.net/cgi-bin/adcycle/adclick.cgi?gid=1&id=305”> which would be rendered as a hyperlink, linking to http://ads.touregypt.net/cgi-bin/adcycle/adclick.cgi?gid=1&id=305 or <table border=”0”> which would style the table with no border. The latter would be better implemented using a cascading style sheet CSS to have distinct documents for content and styling. As it is this document can not be validated as an HTML document as some obvious flaws exist such as document does not have an <HTML> tag which is the root tag for all HTML documents. There are also various tags which are not terminated properly such as the <table border=”0” width=570””> tag.

Document 2

This document is a VALID XML document, although this document follows correct XML syntax, it could not be validated against the referenced DTD schema as this was not supplied.

Document 3

This document is an HTML document, very similar to Document 1, which although makes use of valid HTML tags could not be considered valid XML because of missing <HTML> tags etc.. This document also includes an example of the <script> tag, this time in the header; this is considered better practice than loading javascript in the <body> section as it ensures the script is loaded before the document body. A <form> element is also used in this document, <form> elements allow browsers to capture and forward user inputted data.

Document 4

This document is a VALID XML document; it follows correct XML syntax but cannot be validated against the referenced DTD schema as this was not supplied with the example.

Document 5

This document is an XML document, the first section of the document is the DTD schema which defines the structure of the XML content whilst the second portion of the document is the XML document itself which references the DTD used to validate it.

Document 6

This document is a VALID XML document; it follows correct XML syntax but cannot be validated as the referenced schema was not provided with the examples.

Document 7

This is an HTML document, although most tags defined in this document do not appear to be proper HTML tags such as <OfficialName> & <commonName>, on closer inspection one would notice that these declarations are being passed as string through javascript functions which are then interpreting the data and generating the XML to be rendered to a browser.

Question 2

Make a list of the distinctive characteristics of an XML document, in terms of things that you can spot when looking at the code.

Answer

  • Unlike HTML XML is not limited by a defined set of tags, XML is customizable.
  • Although not strictly required XML documents have corresponding DTD or XSD schemas to be validated against
  • All tags should be follows by corresponding closing tags e.g. <tag>content</tag> or closed immediately e.g. <tag />.
  • XML documents should have one root element.
  • XML tags may contain attributed within the tag e.g. <tag attribute=”value”>
  • Content in XML documents escaped properly otherwise the document may become invalidated e.g. <tag>content > here</tag> is invalid XML the “>” is considered as a special character in XML and should be escaped as follows <tag>content &gt; here</tag>