Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

XML DTD and XML Schema

Lesson 28 of 34 in the free Web Technologies notes on Siksha Sarovar, written by Rohit Jangra.

XML DTD and XML Schema

Why Validate XML?

XML validation ensures that an XML document follows a defined structure and rules. Two main methods:

  1. DTD (Document Type Definition) – older, simpler
  2. XML Schema (XSD) – modern, powerful, XML-based

DTD (Document Type Definition)

Internal DTD

<?xml version="1.0"?>
<!DOCTYPE note [
  <!ELEMENT note (to, from, subject, body)>
  <!ELEMENT to (#PCDATA)>
  <!ELEMENT from (#PCDATA)>
  <!ELEMENT subject (#PCDATA)>
  <!ELEMENT body (#PCDATA)>
]>
<note>
    <to>Alice</to>
    <from>Bob</from>
    <subject>Meeting</subject>
    <body>Let's meet at 3 PM.</body>
</note>

External DTD

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>...</note>

note.dtd:

<!ELEMENT note (to, from, subject, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ATTLIST note date CDATA #REQUIRED>

DTD Element Types

DeclarationMeaning
(#PCDATA)Parsed character data (text)
(a, b)Sequence: a then b
`(ab)`Choice: a or b
element?Optional (0 or 1)
element*Zero or more
element+One or more

XML Schema (XSD)

XSD is more powerful than DTD — it supports data types, namespaces, and complex structures.

<!-- books.xsd -->
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="bookstore">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="book" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="title"  type="xs:string"/>
              <xs:element name="author" type="xs:string"/>
              <xs:element name="year"   type="xs:integer"/>
              <xs:element name="price"  type="xs:decimal"/>
            </xs:sequence>
            <xs:attribute name="category" type="xs:string" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

Referencing XSD in XML:

<bookstore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="books.xsd">
  <book category="fiction">
    <title>The Great Gatsby</title>
    <author>F. Scott Fitzgerald</author>
    <year>1925</year>
    <price>12.99</price>
  </book>
</bookstore>

DTD vs XSD

FeatureDTDXSD
SyntaxNon-XMLXML-based
Data typesNoYes
Namespace supportLimitedFull
ComplexitySimplePowerful
StandardOlderW3C recommended
Key Takeaway: DTD provides basic XML validation with a simple syntax, while XSD (XML Schema) offers full data type support and namespace-awareness. Use XSD for enterprise applications needing strict data contracts.