Web Technologies - Old Questions

2.  What is XML? Discuss the structure of XML file with example. Write an XML schema to describe “address” as an element and “city, state, street and house-no” as its attributes.

10 marks | Asked in 2071-II

XML (Extensible Markup Language) is a software- and hardware-independent tool for storing and transporting data. XML is a markup language that defines a set of rules for encoding documents in a format that is both human readable and machine readable. It has no predefined tags.

The major portions of an XML document include the following:

  • The XML declaration: XML document can optionally have an XML declaration. It must be be the first statement of the XML document. The XML declaration is a processing instruction of the form <?xml........?>
  • The document Type Declaration (DTD): It gives a name to the XML content, and provides a means to guarantee the document's validity either by including or specifying a link to a DTD. It is optional in XML.
  • The element data: XML elements are either a matched pair of XML tags or single XML tags that are "self-closing". For e.g. A address element begins with <address> and ends with </address>. When elements do not come in pairs, the element name is suffixed by the forward slash. XML documents must contain one root element that is the parent of all other elements.
  • The attribute data: It is additional information that can be communicated to XML processors  They are name/value pairs contained within the start element. E.g. <price currency="USD">...</price>
  • The character data or XML content: XML content can consists of any data, including binary data. It can contain any characters including any valid Unicode and International characters.

E.g. of XML

<?xml version="1.0" encoding="UTF-8"?>

<note>

  <to> Sandhya </to>

  <from> Barsha </from>

  <heading> Reminder </heading>

  <body> Don't forget me this weekend! </body>

</note>


XML schema for given elements and attributes

//Address.xml

<?xml version="1.0" encoding="utf-8" ?>

<Information xmlns:xsi="www.w3.org/2001/XMLSchema-Instance" xsi:noNameSpaceSchemaLocation="Address.xsd">

<Address City="Kathmandu" State="Bagmati" Street="Madhevsthan Road" House-No="110"></Address>

</Information>


//Address.xsd

<?xml version="1.0" encoding="utf-8"?>

<xs:schema xmlns:xsd="http://www.w3.or./2001.XMLSchema">

 <xs:element name="Information">

  <xs:complexType>

   <xs:sequence>

    <xs:element name="Address">

     <xs:complexType>

        <xs:attribute name="City" type="xsd:string" use="required">

        <xs:attribute name="State" type="xsd:string" use="required">

        <xs:attribute name="Street" type="xsd:string" use="required">

        <xs:attribute name="House-No" type="xsd:string" use="required">

     </xs:complexType>

    </xs:element>

   </xs:sequence>

  </xs:complexType>

 </xs:element>

</xs:schema>