2082 3

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]

Web TechnologyChapter 5: AJAX and XMLXSD Complex Types

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/AttributeTypeDescription
<library>Root elementContains all book records
<book id="B001">Complex type with attributeid is an attribute of book
<title>, <author>, <name>, <country>StringText values
<price>IntegerNumeric value
<publishedDate>DateDate in YYYY-MM-DD format
<publisher>Complex typeContains 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 ComponentPurpose
xs:schemaRoot element of every XSD file
xs:elementDeclares an XML element
xs:complexTypeDefines elements that contain child elements or attributes
xs:sequenceChild elements must appear in the defined order
xs:attribute use="required"Declares id as a required attribute of <book>
xs:stringSimple type for text values (title, author, name, country)
xs:integerSimple type for whole number values (price)
xs:dateSimple type for date values (publishedDate)
xs:booleanSimple type for true/false values (available)
minOccurs / maxOccursControls 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, and xs: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.