HTML 5 TutorialsXML with DTD Schema

XML with DTD Schema

In this tutorial we are going to create an XML file and a schema as well.
So let’s start with our XML with DTD Schema.

Steps are as follows:

  1. Create a new folder on the desktop named “Sec4_Ch20”.
  2. Now open a new notepad++ file and save it as “movies.xml” in the newly created “Sec4_Ch20” folder.
  3. Write the following code in the file “movies.xml”.
  4. <?xml version="1.0"?>
    		<title>The Godfather</title>
    		<director>Francis Ford Coppola</director>
    		<director>Greg Mottola</director>
    		<title>The Departed</title>
    		<director>Martin Scorsese</director>
    		<title>Saving Private Ryan</title>
    		<director>Steven Spielberg</director>
    		<title>The Expendables</title>
    		<director>Sylvester Stallone</director>
  5. After writing the above code, save the file and open it with internet explorer browser. The whole code is displayed as it is. The movies.xml file opened in IE is shown below:
  6. XML_code_in_IE
    fig 1

    The XML file can be styled using CSS also.

  7. After creating the xml file we would like to validate it. For this we can use xml validators.
  8. Open your browser and search for xml validator.
  9. W3schools has xml validator. You will get a hyperlink “XML Validator – W3Schools”, click on it.
  10. A page will appear which has a block of code below the heading “Syntax – Check Your XML”. A “Validate” button is placed below the code.
  11. Copy the whole code from our “movies.xml” file.
  12. Replace the code in the block of code of xml validator with the copied xml code.
  13. Now click on “Validate” button.
  14. As there is no error in the code, a popup box will appear with a message “No errors found”.
  15. The output is shown below:

    fig 2

  16. Now, purposefully delete one of the tag and again click on “Validate” button. You will get an error along with the line number where error has occurred.
  17. The output is shown below:

    fig 3

    Now it’s time to create a schema using DTD which is Document Type Definition. We will create a separate .dtd file external to the code in movies.xml file.

  18. Create a new text document and name it as “movies.dtd” and open it with notepad++. We can write the DTD code inline to the xml code in the same file i.e. movies.xml also, below the declaration statement.
  19. Type the format that we need in order to validate each element in movies.dtd file. Syntax for validating an element is as follows:
  20. <!ELEMENT ElementName Type>
    • Here, ELEMENT is just a word. ElementName will be the name of the element to be validated.
    • Type can be of many different things:
      • One thing, if an element does not contain content, it should be specified as an empty element in the place of Type.
      • Next, if an element contains other elements and no content, then it should be specified in Type.
      • A mixed type is also there which is a combination of child elements and character data.
      • Last type can be anything, any content allowed by DTD.
      • Let us understand a code with xml and DTD schema. Just have a look on the code given below:
      • <?xml version="1.0"?>
        <!DOCTYPE note [
        <!ELEMENT note (to,from,heading,body)>
        <!ELEMENT to (#PCDATA)>
        <!ELEMENT from (#PCDATA)>
        <!ELEMENT heading (#PCDATA)>
        <!ELEMENT body (#PCDATA)>
        	<message>Don't forget me this weekend!</message>
      • In the above code, the statements in

        tags are the xml statements,


        being the root element.

      • The statements starting with

        are DTD schema statements validating xml part of the file.

      • <note></note>

        contains child elements in it like to, from, heading, etc.

      • To validate each xml element we use DTD schema. For root element note, the DTD schema
        <!ELEMENT note (to,from,heading,body)>

        is used. Here, note is the root element and this root element does not directly contain data, it contains child elements. So these child elements are specified as type in the paranthesis i.e. (to, from, heading, body).

      • Next the child elements directly contain data in it, so it has #PCDATA as the type. #PCDATA means parse data that can be read by the system.
      • All the child elements that contain directly the content without any other element has the type as #PCDATA.
  21. Let’s write our own DTD document. Open our “movies.dtd” file, write the following code in it and save the file:
  22. <!DOCTYPE movies[
    	<!ELEMENT movies (movie)>
    	<!ELEMENT movie (title,year,genre,director)>
    	<!ELEMENT title (#PCDATA)>
    	<!ELEMENT year (#PCDATA)>
    	<!ELEMENT genre (#PCDATA)>
    	<!ELEMENT director (#PCDATA)>
  23. We have our xml code in movies.xml file and DTD schema in movies.dtd file. This DTD schema will validate the xml code only if both the files are linked to each other. So to link the two files, we need to declare the file movies.dtd in the file movies.xml below the xml declaration statement as shown below:
  24. <!DOCTYPE movies SYSTEM “movies.dtd”

    Thus we declared the external DTD file in the xml file.

  25. Now again follow the steps from 6 to 11 and see if our movies.xml file is validated or not. Replace the DTD linking statement in movies.xml code with the DTD schema code in the xml validator block for validating purpose.Here we are validating our XML with DTD Schema

Thus we created our XML with DTD Schema.


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -