CSC329 ยท 2082 ยท 10 marks
What is the use of XML? Create an XML file having complex type elements containing other elements and text. At least one element should contain an attribute. Similarly, there should be elements containing string, integer and date type values. Now write the equivalent XSD for the XML.
What is the use of XML? Create an XML file having complex type elements containing other elements and text. At least one element should contain an attribute. Similarly, there should be elements containing string, integer and date type values. Now write the equivalent XSD for the XML.[10]
Quick answer
The following XML file represents a Library with Book records. It includes: - Complex type elements containing other elements and text - At least one element with an attribute - Elements with string, integer, and date type values
XML: Uses, XML File with Complex Types, and Equivalent XSD
Uses of XML
XML (eXtensible Markup Language) is used for:
- Data storage and transport between applications and systems
- Platform-independent data exchange over the internet
- Configuration files for software applications (e.g., web.xml, pom.xml)
- Web services (SOAP, REST use XML for message formatting)
- Document representation (e.g., Office documents, RSS feeds)
- Database-like storage for small structured datasets
- Separating data from presentation (used with XSLT for transformation)
XML File with Complex Type Elements
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book id="B001">
<title>Introduction to Programming</title>
<author>Dennis Ritchie</author>
<publisher>
<name>TechPress</name>
<country>USA</country>
</publisher>
<price>1500</price>
<publishedDate>2020-06-15</publishedDate>
<available>true</available>
</book>
<book id="B002">
<title>Data Structures and Algorithms</title>
<author>Thomas Cormen</author>
<publisher>
<name>MIT Press</name>
<country>USA</country>
</publisher>
<price>2200</price>
<publishedDate>2018-03-10</publishedDate>
<available>false</available>
</book>
</library>
Explanation of the XML
| Element/Attribute | Type | Description |
|---|---|---|
<library> | Root element | Contains all book records |
<book id="B001"> | Complex type with attribute | id is an attribute of book |
<title>, <author>, <name>, <country> | String | Text values |
<price> | Integer | Numeric value |
<publishedDate> | Date | Date in YYYY-MM-DD format |
<publisher> | Complex type | Contains child elements <name> and <country> |
Equivalent XSD (XML Schema Definition)
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Root Element -->
<xs:element name="library">
<xs:complexType>
<xs:sequence>
<xs:element name="book" type="bookType"
minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- Complex Type: bookType -->
<xs:complexType name="bookType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="publisher" type="publisherType"/>
<xs:element name="price" type="xs:integer"/>
<xs:element name="publishedDate" type="xs:date"/>
<xs:element name="available" type="xs:boolean"/>
</xs:sequence>
<!-- Attribute for book element -->
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
<!-- Complex Type: publisherType (nested complex type) -->
<xs:complexType name="publisherType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Explanation of the XSD
| XSD Component | Purpose |
|---|---|
xs:schema | Root element of every XSD file |
xs:element | Declares an XML element |
xs:complexType | Defines elements that contain child elements or attributes |
xs:sequence | Child elements must appear in the defined order |
xs:attribute use="required" | Declares id as a required attribute of <book> |
xs:string | Simple type for text values (title, author, name, country) |
xs:integer | Simple type for whole number values (price) |
xs:date | Simple type for date values (publishedDate) |
xs:boolean | Simple type for true/false values (available) |
minOccurs / maxOccurs | Controls how many times an element can appear |
Note: XSD is like DTD but provides more control on XML structure. It supports predefined simple types such as
xs:integer,xs:boolean,xs:string, andxs:date, and uses complex types as containers for other element definitions to build a hierarchy of elements.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.