CSC329 ยท 2079 ยท 10 marks
What is the use of XML? Create a XML file with simple type and complex type elements. Write its equivalent DTD.
What is the use of XML? Create a XML file with simple type and complex type elements. Write its equivalent DTD.[10]
Quick answer
XML (eXtensible Markup Language) is a markup language designed to store, transport, and structure data in a human-readable and machine-readable format.
Use of XML, XML File with Simple/Complex Types, and Equivalent DTD
Uses of XML
XML (eXtensible Markup Language) is a markup language designed to store, transport, and structure data in a human-readable and machine-readable format. Its main uses are:
| Use | Description |
|---|---|
| Data Storage | Stores data in a structured, self-describing format |
| Data Transport | Exchanges data between incompatible systems and platforms |
| Configuration Files | Many applications use XML for settings and configuration |
| Web Services | SOAP-based web services use XML for request/response messages |
| Separation of Data from Presentation | XML stores data; HTML/CSS handles display |
| Platform Independence | Works across different operating systems and programming languages |
| Document Representation | Used in formats like DOCX, SVG, and RSS feeds |
XML File with Simple Type and Complex Type Elements
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE library SYSTEM "library.dtd">
<library>
<book id="101">
<title>Web Technology</title>
<author>
<firstname>John</firstname>
<lastname>Smith</lastname>
</author>
<price>450.00</price>
<year>2020</year>
</book>
<book id="102">
<title>XML Fundamentals</title>
<author>
<firstname>Mary</firstname>
<lastname>Jones</lastname>
</author>
<price>350.50</price>
<year>2021</year>
</book>
</library>
Explanation of Element Types
Simple Type Elements contain only text/data and no child elements:
<title>,<firstname>,<lastname>,<price>,<year>
Complex Type Elements contain other child elements or attributes:
<library>contains one or more<book>child elements<book>contains child elements and has an attributeid<author>contains<firstname>and<lastname>child elements
: "Simple type element is used only in the context of text" and "A complex type is a container for other element definitions, allowing us to specify which child elements an element can contain."
Equivalent DTD (Document Type Definition)
<!ELEMENT library (book+)>
<!ELEMENT book (title, author, price, year)>
<!ATTLIST book id CDATA #REQUIRED>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (firstname, lastname)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT year (#PCDATA)>
Explanation of DTD Declarations
| DTD Declaration | Meaning |
|---|---|
<!ELEMENT library (book+)> | library contains one or more book elements (+ means one or more) |
<!ELEMENT book (title, author, price, year)> | book is a complex type with four ordered child elements |
<!ATTLIST book id CDATA #REQUIRED> | book has a required attribute id of character data type |
<!ELEMENT title (#PCDATA)> | title is a simple type containing parsed character data (text only) |
<!ELEMENT author (firstname, lastname)> | author is a complex type with two child elements |
<!ELEMENT firstname (#PCDATA)> | firstname is a simple type containing text only |
<!ELEMENT lastname (#PCDATA)> | lastname is a simple type containing text only |
<!ELEMENT price (#PCDATA)> | price is a simple type containing text only |
<!ELEMENT year (#PCDATA)> | year is a simple type containing text only |
: "In DTD, attributes are declared with an ATTLIST declaration" with syntax
<!ATTLIST element-name attribute-name attribute-type attribute-value>.
Summary
- XML provides a flexible, platform-independent way to structure and exchange data.
- Simple type elements hold only text data (
#PCDATA). - Complex type elements contain other child elements or attributes, acting as containers.
- DTD defines the legal structure and rules for the XML document, ensuring data validity and consistency.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.