Introduction to XML


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)>

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 or for the curriculum XML. Use The W3 on-line validator is located on 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 to create and validate xml files.

If you use an XML editor like the one that comes with Visual Studio, it does the validation during editing.

Practice 4f: learning to read a schema

Create an XML document instance with your own data to conform to the given schema. Use any editor of your preference, for example Eclipse. The schema is called contacts.xsd. Add at least 3 people to it.
Your XML document instance might look something like this: example.
Modify the schema and add a gender attribute (required) that can have two possible values: male and female.
Note that the contact can have only one phone number according to the schema definition. Change it so that there can be more than one phone number, as well as an email address and possibly also a Web-site. Modify your document instance accordingly.

One validator is
another validator:
another validator:

Or use Eclipse or to create and validate xml files.

Practice 4g: XML Schema design

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.


Created by: Jaana Holvikivi
Updated: 31.3.2015