Web Technology - Old Questions

2. Define XML schema. Compare it with DTD. Write a schema to describe "name" as a root element and "first_name","middle_name" and "last_name" as child elements.

10 marks | Asked in 2074

XML schema is a language which is used for expressing constraint about XML documents. It is used to define the structure of an XML document. It is like DTD but provides more control on XML structure.

Comparison between DTD and XML schema

  • XML schemas are written in XML while DTD are derived from SGML syntax.
  • XML schemas define datatypes for elements and attributes while DTD doesn't support datatypes.
  • XML schemas allow support for namespaces while DTD does not.
  • XML schemas define number and order of child elements, while DTD does not.
  • XML schemas can be manipulated on your own with XML DOM but it is not possible in case of DTD.
  • using XML schema user need not to learn a new language but working with DTD is difficult for a user.
  • XML schema provides secure data communication i.e. sender can describe the data in a way that receiver will understand, but in case of DTD data can be misunderstood by the receiver.
  • XML schemas are extensible while DTD is not extensible.

XML schema to describe given elements

//name.xml

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

<name xsi:schemalocation="name.xsd">

    <first_name>Surya</first_name>

    <middle_name>chandra</middle_name>

    <last_name>Basnet</last_name>

</name>


// name.xsd

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

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

<xs:element name="name">

  <xs:complexType>

    <xs:sequence>

      <xs:element name="first_name" type="xs:string"/>

      <xs:element name="middle_name" type="xs:string"/>

      <xs:element name="last_name" type="xs:string"/>

    </xs:sequence>

  </xs:complexType>

</xs:element>

</xs:schema>