Quick Questions
Question 1
People who prepare XML documents sometimes put part of the document in a CDATA section
a) Why would they do that?
b) How is the CDATA section indicated?
c)If CDATA sections hadn’t been invented, would there be any other way to achieve the same effect?
Answer 1 a
CDATA is used so that people can put code in this section, any code in this section is not parsed but is read as any other character. Any text placed here is "dumped".
Answer 1b
<![CDATA [content-this won't be parsed]]>
Answer1c
One can also use another technique like UTF-8. This is only possible if illegal characters are escaped. If you want to use the '&' sign this should be done using '&'
Question 2
What is a parser and what does it have to do with validity?
Answer
A parser is a program that passes through the code fragments it and understands the sytnax in order to be able to do something with it.
Example
dbname = cynthia
username = cyn
password = cyncyn
The program which goes through this document to gather this data is a parser. An XML parses understands XML syntax.
Question 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?
Answer
With XML, displaying the <trinity> for several times cannot be done, however it can be done with the use of XSD.
Longer Questions:
Question 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.
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.
Answer 1a
<?xml version = “1.0” encoding = “UTF-8”?>
<!DOCTYPE section2 SYSTEM “section2.dtd”
Answer 1b
<!DOCTYPE section2
[
<!ELEMENT chapter (paragraphs)>
<!ATTLIST chapter chapterId CDATA #REQUIRED chapterTitle CDATA #REQUIRED>
<!ELEMENT paragraphs (section+)>
<!ELEMENT section (poem, image*)>
<!ELEMENT section (#PCDATA)>
<!ELEMENT poem (stanza+)>
<!ELEMENT stanza (#PCDATA)>
<!ELEMENT image EMPTY>
<!ATTLIST image caption CDATA #REQUIRED src CDATA #REQUIRED>
]>
Answer 1c
<?xml version ="1.0" encoding="UTF-8"?>
<!DOCTYPE section2 SYSTEM "section2.dtd">
<chapter chapterID="2" chapterTitle ="Chapter2: Volcanic Winter">
<paragraphs>
<paragraph>
</paragraph>
<paragraph>
<poem>
<stanza>The bright sun was extinguish'd, and the stars Did wander darkling in the eternal space,</stanza>
<stanza>Rayless, and pathless, and the icy earth Swung blind and blackening in the moonless air;</stanza>
<stanza>Morn came and went – and came, and brought no day.</stanza>
</poem>
</paragraph>
<paragraph>
</paragraph>
</paragraphs>
[
<!ELEMENT chapter (paragraphs)>
<!ATTLIST chapter chapterId CDATA #REQUIRED chapterTitle CDATA #REQUIRED>
<!ELEMENT paragraphs (section+)>
<!ELEMENT section (poem, image*)>
<!ELEMENT section (#PCDATA)>
<!ELEMENT poem (stanza+)>
<!ELEMENT stanza (#PCDATA)>
<!ELEMENT image EMPTY>
<!ATTLIST image caption CDATA #REQUIRED src CDATA #REQUIRED>
]>
Answer 1c
<?xml version ="1.0" encoding="UTF-8"?>
<!DOCTYPE section2 SYSTEM "section2.dtd">
<chapter chapterID="2" chapterTitle ="Chapter2: Volcanic Winter">
<paragraphs>
<paragraph>
A volcanic winter is very bad news. The worst eruption in recorded history happened at Mount Tambora in 1815. It killed about 71 000 people locally, mainly because the pyroclastic flows killed everyone on the island of Sumbawa 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. The next year was the year without a summer. 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. The Lakeland poets took a holiday at Lake Geneva, and the weather was so horrible that Lord Byron was inspired to write this.
<image src ="Sumbawa.jpg" caption="Subawa, after the volcanic eruption"></paragraph>
<paragraph>
<poem>
<stanza>The bright sun was extinguish'd, and the stars Did wander darkling in the eternal space,</stanza>
<stanza>Rayless, and pathless, and the icy earth Swung blind and blackening in the moonless air;</stanza>
<stanza>Morn came and went – and came, and brought no day.</stanza>
</poem>
</paragraph>
<paragraph>
Mary Shelley was inspired to write Frankenstein. The downside was that there were famines throughout Europe, India, China and North America, and perhaps 200 000 people died of starvation in Europe alone.
<image src = "Mary.jpg" caption="Mary Shelly: author of Frankenstein"></paragraph>
</paragraphs>