Wednesday, 23 November 2011

Lab 4: Well-Formed XML

Here are the Questions given in the 4th Lab session of this course. These questions mostly dealt with well-formness of XML


Quick questions:

  1. What does XML stand for? And CSS?

XML stands for Extensible Markup Language and is designed to transport and store data.
CSS stands for  Cascading Style Sheets which is used to store formatting information of HTML pages.


  1. Is this XML line well-formed? Say why.

<b><i>This text is bold and italic</b></i>

No, this is not a well-formed XML line as the closing tags do not match well with the opening tags. To be well-formed this XML line must be changed to:

<b><i>This text is bold and italic</i></b>


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

<?xml version= “1.0” ?>
<greeting>
Hello, world!
</greeting>
<greeting>
Hello Mars too!
</greeting>

No, this isn’t a well formed XML document either as there is no root element in this XML. For the document to be well formed it has to be changed to something like:

<?xml version= “1.0” ?>
<differentGreetings>
<greeting>Hello, world!</greeting>
<greeting>Hello Mars too!</greeting>
</differentGreetings>



Longer questions:

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

<course>
     <name>Advanced Web Technologies</name>
<building>STC</building>
     <session starttime=”18:00” endtime=”21:00”>
         <room>1</room>
     </session>
</course>

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

<?xml version= “1.0” ?>
<!DOCTYPE bookStock SYSTEM "bookstock.dtd">
<bookstore>
  <book category="Cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <1stEdition>2005</1stEdition >
    <2ndEdition>2007</2ndEdition >
    <price>19.99</price currency=”pounds sterling”>
  </book>
  <book category="Children’>
    <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>
    <price>29.99</Price>
  </book>
  <book category="Web">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
   <1stEdition>2003</1stEdition>
   <2ndEdition >2008</2ndEdition >
    <price>29.95</discount>
    <discount>15%</price>
  </book>
  <book category="Computing">
    <title lang=en>Insanely great – the life and times of Macintosh, the computer that changed everything </title>
    <author 
<!—other authors not listed -->>Steven Levy</author>
   <1stEdition>1994</1stEdition>
    <price>9.95</discount>
    <discount>15%</price>
  </book>



Error 1: Tags cannot start with a numeric value
Ex: <1stEdition>2005</1stEdition >

Error 2:  Attributes cannot be placed at closing tags
Ex: <price>19.99</price currency=”pounds sterling”>

Error 3:     XML tags are Case sensitive
Ex:<price>29.99</Price>

Error 4 :   Tags are not correctly matched
Ex:  <price>29.95</discount>
       <discount>15%</price>

Error 5 : Attributes must be declared in qoutes
Ex :<title lang=en >Insanely great – the life and times of Macintosh, the computer that changed everything </title>

Error 6:   A comment cannot be place inside a tag
Ex: <author <!—other authors not listed -->>Steven Levy</author>

Error 7: There is no closing tag for <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:

<!DOCTYPE memo
[
<!ELEMENT memo (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>

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 file so that it did what the client wanted?

Basically we have to add 3 things to the DTD provided:

First we have to add another element for the Date, so this:

<!ELEMENT memo (to,from,heading,body)>

Has to be changed to:

<!ELEMENT memo (to,from,heading,body,date)>

Also after the last element we have to add:

<!ELEMENT date (#PCDATA)>


The 2nd item to add is the serial number attribute. This is acheived by creating an ATTLIST after declaring all the Elements as follow:

<!ATTLIST memo
     serialNumber ID #REQUIRED
> 


The last item to add is the classification attribute to know if the memo is a secret. Again we have to add this in the ATTLIST but this attribute will be an enumerator, so only specific values will be accepted:

Classification (Secret|Public) “Security”

So the final DTD will look like this:

<!DOCTYPE memo
[
<!ELEMENT memo (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ATTLIST memo
     serialNumber ID #REQUIRED
     classification (Secret|Public) “Security”
>
]>

No comments:

Post a Comment