XML with DTD Schema

0
2546

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. 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:
  5. XML_code_in_IE
    fig 1

    The XML file can be styled using CSS also.

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

    validated_xml_code
    fig 2

  15. 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.
  16. The output is shown below:

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

  17. 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.
  18. Type the format that we need in order to validate each element in movies.dtd file. Syntax for validating an element is as follows:
    • 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:
      • 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.

      • 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 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.
  19. Let’s write our own DTD document. Open our “movies.dtd” file, write the following code in it and save the file:
  20. 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:
  21. Thus we declared the external DTD file in the xml file.

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here