Web Technologies - Old Questions

3.  What are elements and attributes of XML document? What is XML schema? Differentiate XML schema with DTD. Write a simple example of DTD.

10 marks | Asked in 2072

XML elements and attributes

XML elements can be defined as building blocks of an XML. Elements can behave as containers to hold text, elements, attributes, media objects or all of these. Each XML document contains one or more elements, the scope of which are either delimited by start and end tags, or for empty elements, by an empty-element tag.

XML attributes are part of XML elements. An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements. An XML attribute is always a name-value pair.

Sytnatx:

<element-name attribute1 attribute2>

    ....content

</element-name>

where,

  • element-name is the name of the element. The name its case in the start and end tags must match. An empty element (element with no content) has following syntax −

            <name attribute1 attribute2.../>

  • attribute1, attribute2 are attributes of the element separated by white spaces. An attribute defines a property of the element. It associates a name with a value, which is a string of characters. An attribute is written as − name = "value"

E.g.

<?xml version = "1.0"?>

<contact-info>

   <address category = "residence">    //category is attribute

      <name> Alisha </name>

      <phone>01-1234567</phone>

   </address>

</contact-info>


Difference between XML schema and DTD

  • 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.


Example of DTD

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

<!DOCTYPE mail[

<!ELEMENT mail(to, from, subject, message)>

<!ELEMENT to(#PCDATA)>

<!ELEMENT from(#PCDATA)>

<!ELEMENT subject(#PCDATA)>

<!ELEMENT message(#PCDATA)>

]>

<mail>

    <to> Jayanta </to>

    <from> Arjun </from>

    <subject> webtech </subject>

    <message> Never miss the web tech class </message>

</mail>