Saturday, December 17, 2011

8 - CSS & XML


Quick questions:

1. You have a set of legal documents. Each has four sections: the title, the case, the background, and the judgement, in that order. Each has been made into an XML document by inserting a prolog and suitable tags. You want to write a CSS file that will display these documents using a suitable browser.

a. Can you write the CSS file in such a way that it will display the title, then the judgement, then the background, then the case?

CSS is meant to style your data not arrange the order of the content. But this can be done by using absolute positioning to position the judgement right after the title.

b. Can you write the CSS file in such a way that it will display just the title, and the judgement?

In CSS the "display: none" property can be referenced to hide the unwanted elements.

c. If the CSS file is called legalWrit.css, what processing instruction should you put in the prolog of the XML document(s)?

<?xml-stylesheet type="text/css" href="legalWrit.css"?>

2. What is the difference between a URI and a URL?

A URI defines a resource by location or by name. URI refer to a resource by not specifying it's extension. A URL is a URI but a URI is not a URL. A URL is a subset of URI that defines the network location of a specific representation for a given resource. URIs also define the file extension that indicates what content type is available at the URL.

3. Why does the XML language allow namespaces?

Namespaces are used to identify the context for the elements. An example of this would be: bird wings and aeroplane wings. They are both wings but the context is much different. Namespaces define an ontology.

Example:

<aeroplane:wings></aeroplane:wings>
<bird:wings></bird:wings>

Longer questions:

1. Here is a short XML document. Type it out, as a new file in JCreator. Save it under the name memo1.xml in a suitable directory in your file system. Notice that the JCreator editor picks out the different components in different colours, to aid you in detecting errors.

<?xml version="1.0"?>
<?xml-stylesheet href="stylesheet01.css" type="text/css"?>
<!DOCTYPE memo>
<memo>
            <id>Message: 1334</id>
            <date>18 November 09</date>
            <time>09:30</time>
            <from>From: The Managing Director</from>
            <to>To: Heads of all Departments</to>
            <message>We must increase production. And increasing sales would be no bad thing either.</message>
</memo>

Now open another tab in JCreator and type the following style sheet out. Save it under the name stylesheet01.css in the same folder as memo1.xml. Notice that, this time, the editor does not pick out the different components in different colours.

memo {display: block; margin: 1em;}
id {display: block; margin: 1em; font-style: italic; font-size:200%}
date {display: block; margin: 1em;color: "Blue"; text-align: left;}
time {display: block; margin: 1em;color: aqua; text-align: left;}
from, to {display: block; margin: 1em;color: green; text-align: left;}
message {display: block; margin: 1em;color: blue; text-align: left;}

Now use the Mozilla Firefox browser to view the file memo1.xml.
What was the point of putting “display: block” into the CSS file in each of the 6 lines?


"display:block" puts a block around our elements. It specifies whitespace above and below the element and does not allow any elements next to it. This is particularly useful when displaying headers or titles.

2. We want the chapter we were working on last week (“Chapter 2: Volcanic winter”) to be displayed on screen in a web browser. Here are some of the features we would like it to have: the font for the text to be Palatino, or failing that Times New Roman, or failing that any serif face. Type size to be 12 pt. The chapter heading to be the same font, but 24 pt and bold and italic and blue. The poem lines to be the same font, but italic. Background colour to be parchment: use the colour #FCFBC4. Both the chapter heading and the main text are to be indented from the left margin by 1 em. The lines of poetry are to be indented from the left margin by 2 ems.

a) Write a CSS file that will enable the chapter to be displayed in this way. Call it stylesheet4.css

chapter{
    background-color: #FCFBC4;
    font-family: Palatino, "Times New Roman", Serif;
    font-size:12pt;
    }
title{
    font-weight:bold
    font-size:24pt;
    font-style:italic
    color:blue;
    margin-left:1em;
    display:block;
    }
text{
    padding-top: 5px;
    margin-left:1em;
    display:block;
    }
verse{
    padding-top: 3px;
    font-style:italic
    margin-left:2em;
    display:block;
    margin-top: 5px;
    margin-bottom: 5px;
    }

b) Type some – not all – of the XML document version of “Chapter 2: Volcanic winter” into a suitable file. Store it in the same folder as the stylesheet4.css document you have just written. View the file, using the Mozilla Firefox browser. See whether it looks as it should. If not, change the CSS file and view it again. Repeat this until you have it right.



c) Right a different CSS file, with different display properties, and adjust your XML file so that it is displayed using this one instead. Use display properties that seem appropriate to you.

chapter{
    background-color: #FCFBC4;
    font-family: Palatino, "Times New Roman", Serif;
    font-size:12pt;
    }
title{
    font-weight:bold
    font-size:24pt;
    font-style:italic
    color:blue;
    margin-left:1em;
    display:block;
    text-align: center;
    margin-bottom: 10px;
    }
text{
    padding-top: 5px;
    margin-left:1em;
    display:block;
    }
verse{
    padding-top: 3px;
    font-style:italic
    margin-left:2em;
    display:block;
    margin-top: 5px;
    margin-bottom: 5px;
    }
poem{
    display: block;
    margin-top: 15px;
    margin-bottom: 15px;
    text-align: center;
}
indexEntry{
    color:Gray;
}



Thursday, December 8, 2011

7 - DTDs


Quick questions:

1.    People who prepare XML documents sometimes put part of the document in a CDATA section.

a. Why would they do that?

CDATA is used to indicate text that needs to be ignored by the XML parser. This is done when the text will include characters used be XML such as "<" or ">".

b. How is the CDATA section indicated?

A CDATA section is indicated by the use of: <! [CDATA [Your Text ...]]> 

c. If CDATA sections hadn't been invented, would there be any other way to achieve the same effect?
The use of text commenting can be applied for text that is not to be parsed, but it does not allow characters used by XML such as "<" or ">". However, the escaping technique could be used to include these characters by replacing "<" with "&lt" and ">" with "&gt" for example. This would require some time and effort and would make the text slightly difficult to be understood by humans.

2. What is a parser and what does it have to do with validity?

There are two types: validating and non-validating parsers. A non-validating parser checks the well-formedness of the XML document. On the other hand, a validating parser will check and validate the structure of the document according to a specified schema. Parsers are able of doing tasks such as reading and extracting the data and identifying special statements such as the prolog and DTD declaration.

3. You write a .dtd file to accompany a class of XML documents. You want one of the elements, with the tag <trinity>, to appear exactly three times within the document element of every document in this class. Is it possible for the .dtd file to specify this?

In a DTD you cannot specify exactly the nth time an element should appear. Despite this, one can include three sub-elements of <trinity>  named <trinity1><trinity2><trinity3> to overcome this.

Longer questions:

1. The following is one of the documents that featured in last week’s exercises. As mentioned before, this is to be “Chapter 2: Volcanic winter” in a book.

a) Write a suitable prolog for this document.

b) Write a .dtd file to act as the Document Type Description for this document. Or modify the one you wrote last week, if you wrote one.

c) Put tags into the document. Obviously, there must be a document element. But also, the poem needs special treatment (because of the way it will be displayed) and, in fact, each line of the poem needs special treatment (you can spot the places where the lines start, by the capital letters). The mention of the poets at Geneva needs to be identified, because it will feature in the index, and so do the pyroclastic flows and Mount Tambora and Sumbawa and the year without a summer and the famines.

2. This chapter obviously needs some pictures. You have available the following, and you decide to include them in the chapter, at appropriate places:
  • a picture of Sumbawa, after the volcanic eruption. It’s in a file sumbawa.jpg. Caption: “Sumbawa, after the volcanic eruption”.
  • a picture of Lake Geneva, in 1816. It’s in a file Geneva1816.jpg. Caption: “Lake Geneva, during the summer of 1816”.
  • a picture of Mary Shelley. It’s in a file MaryShelley.jpg. Caption: “Mary Shelley, author of Frankenstein”.

Amend your two files so that they can cope with these pictures and captions.

DTD File:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE chapter [
<!ELEMENT chapter (title, section)>
<!ELEMENT section (text | poem)*>
<!ELEMENT poem (verse+)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT text (#PCDATA | indexEntry)*>
<!ELEMENT indexEntry (#PCDATA)>
<!ELEMENT verse (#PCDATA)>
<!ATTLIST chapter chapterNumber ID #REQUIRED>
]>

XML File:


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE chapter>

<chapter chapterNumber="Two">

            <title>Chapter Two: Volcanic winter</title>
            <section>
                        <text>A volcanic winter is very bad news. The worst eruption in recorded history happened at <indexEntry> Mount Tambora</indexEntry> in 1815. It killed about 71 000 people locally, mainly because the <indexEntry>pyroclastic</indexEntry>  flows killed everyone on the island of <indexEntry>Sumbawa</indexEntry> and the tsunamis drowned the neighbouring islands, but also because the ash blanketed many other islands and killed the vegetation. It also put about 160 cubic kilometres of dust and ash, and about 150 million tons of sulphuric acid mist, into the sky, which started a volcanic winter throughout the northern hemisphere.</text>
                        <img src="/sumbawa.jpg" cap="Sumbawa, after the volcanic eruption"/>
                        <text>The next year was the <indexEntry>year without a summer</indexEntry>. No spring, no summer – it stayed dark and cold all the year round. This had its upside. In due course, all that ash and mist in the upper atmosphere made for some lovely sunsets, and Turner was inspired to paint this.</text>
                        <text>The Lakeland poets took a holiday at <indexEntry>Geneva</indexEntry>, and the weather was so horrible that Lord Byron was inspired to write this. </text>
                        <img src="/Geneva1816.jpg" cap="Lake Geneva, during the summer of 1816"/>
                        <poem>
                                    <verse>The bright sun was extinguish'd, and the stars </verse>
                                    <verse>Did wander darkling in the eternal space,</verse>
                                    <verse>Rayless, and pathless, and the icy earth</verse>
                                    <verse>Swung blind and blackening in the moonless air;</verse>
                                    <verse>Morn came and went—and came, and brought no day,</verse>
                        </poem>
                        <text>Mary Shelley was inspired to write Frankenstein.</text>
                        <img src="/MaryShelley.jpg" cap="Mary Shelley, author of Frankenstein"/>
                        <text>The downside was that there were <indexEntry>famines</indexEntry> throughout Europe, India, China and North America, and perhaps 200 000 people died of starvation in Europe alone.</text>
            </section>
</chapter>