Tuesday, November 15, 2011

4 - Well-formedness and Valid XML Syntax


Quick questions:

1. What does XML stand for? And CSS?

XML stands for Extensible Markup Language. XML is used to create common information formats usually to be used where data needs to be shared. CSS, on the other hand stands for Cascading Style Sheets. It is used to define styles for webpages.

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

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

This XML is not well formed since the closing </i> tag is closed after the </b>

A correct way to do this would be:

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

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

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

This XML document is not well formed since there is no root element defined. A corrected version would be:

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


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.

<CourseList>
            <Course>
                        <Name>Advanced Web Technologies</Name>
                        <Building>STC Training, Malta</Building>
                        <Sessions>
                                    <Session>
                                                <Room>50</Room>
                                                <StartTime>18:00</StartTime>
                                                <EndTime>21:00</EndTime>
                                    </Session>
                        </Sessions>
            </Course>
</CourseList>

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>

A corrected version of the XML document is shown below, along with some remarks:
  • In line 7,8 and 15 tags cannot start with a number
  • In line 9, attributes must be placed in the starting tag, not the end tag
  • Line 11, uses the wrong quotation mark of ' instead of "
  • In line 13, comments must start with "<!--" and cannot contain another "--" unless it is the ending tag
  • There is no ending tags for the book node or the root node bookstore
<?xml version="1.0"?>
<!DOCTYPE bookStock>
<bookstore>
            <book category="Cooking">
                        <title lang="en">Everyday Italian</title>
                        <author>Giada De Laurentiis</author>
                        <FirstEdition>2005</FirstEdition>
                        <SecondEdition>2007</SecondEdition>
                        <price currency="pounds sterling">19.99</price>
            </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>
                        <FirstEdition>2005</FirstEdition>
                        <price>29.99</price>
            </book>
            <book category="Web">
                        <title lang="en">Learning XML</title>
                        <author>Erik T. Ray</author>
            </book>
</bookstore>



No comments:

Post a Comment