Introduction to XML

| HOME | SYLLABUS | CALENDAR AND EXERCISES |

DTD and XML Schema

Practice 4a-c: Simple document type definitions

Create XML document instances to conform to the given DTD definitions, two for each. Note that these are really simple files.
Include the DTD to the XML document (as an internal subset of the structure). The beginning of the file looks like:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE seat [ <!ELEMENT seat (person?) >
<!ELEMENT person (#PCDATA)>
]>
<seat>
....
</seat>

a) <!ELEMENT seat ( person?) >

b) <!ELEMENT course ( number, (teacher | assistant+), (credit | non-credit )) >

c) <!ELEMENT farm (farmer+, (dog* | cat?), pig*, (goat | sheep )?, (hen+| goose*) )>

Save and test with a browser, for example IE.

Practice 4d:

Why is the following document instance not valid if the DTD is like the DTD here? Make the necessary corrections and indicate your corrections by a comment.

Practice 4e: XML phonebook 

In an earlier exercise you created a phone book XML markup. Create now a DTD for it. Use The W3 on-line validator is located on http://validator.w3.org/ to validate it. Make the DTD internal for the validator. Note also,  that it requires that each element is explicitly defined like below:

<!ELEMENT sense (definition, example*)>
<!ELEMENT definition (#PCDATA)>
<!ELEMENT example (#PCDATA)>

Or use http://codeplot.com to create and validate xml files.

Another on-line validator is http://valet.webthing.com/page, which requires an URL, thus you must post the document on the Web. The Brown University online validator is http://www.stg.brown.edu/service/xmlvalid/. If you use an XML editor like the one that comes with Visual Studio, it does the validation during editing.

Practice 4f: XML Schema

Now try to create a schema for the phonebook or restaurant menu that you drafted earlier. Does this change the original design? You can try the Visual Studio .NET Schema generator by opening the XML file in Visual Studio and select XML and Create schema, but note that the resulting schema needs tuning.

Check the validity using http://www.w3.org/2001/03/webdata/xsv .

| HOME | SYLLABUS | CALENDAR AND EXERCISES |

Created by: Jaana Holvikivi
Updated: 23.1.2009