1. Home
  2. Docs
  3. Web Technology I
  4. eXtensible Markup Languag...
  5. DTD and Schema

DTD and Schema

Here is the explanation of DTD and Schema:

<!DOCTYPE bookstore [
    <!ELEMENT bookstore (title, author, price)>
    <!ELEMENT title (#PCDATA)>
    <!ELEMENT author (#PCDATA)>
    <!ELEMENT price (#PCDATA)>
]>
  • <!DOCTYPE bookstore– Defines that the root element of the document is bookstore
  • <!ELEMENT bookstore– Defines that the bookstore element must contain the elements: “title, author, price”
  • <!ELEMENT title– Defines the title element to be of type “#PCDATA
  • <!ELEMENT author– Defines the author element to be of type “#PCDATA
  • <!ELEMENT price– Defines the price element to be of type “#PCDATA
  • Internal DTD
  • External DTD.

<!DOCTYPE bookstore [
    <!ELEMENT bookstore (book+)>
    <!ELEMENT book (title, author, price)>
    <!ELEMENT title (#PCDATA)>
    <!ELEMENT author (#PCDATA)>
    <!ELEMENT price (#PCDATA)>
]>

<bookstore>
    <book>
        <title>Introduction to XML</title>
        <author>John Doe</author>
        <price>29.95</price>
    </book>
   
</bookstore>
<!ELEMENT bookstore (book+)>
<!ELEMENT book (title, author, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!DOCTYPE bookstore SYSTEM "books.dtd">

<bookstore>
    <book>
        <title>Introduction to XML</title>
        <author>John Doe</author>
        <price>29.95</price>
    </book>
    <!-- More book elements can follow -->
</bookstore>
<!-- bookstore.xsd -->

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

  <!-- Define the elements -->
  <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="price" type="xs:decimal"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>
<!-- books.xml -->

<bookstore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="bookstore.xsd">

  <book>
    <title>Introduction to XML</title>
    <author>John Doe</author>
    <price>29.95</price>
  </book>

  <!-- More book elements can follow -->

</bookstore>

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *