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:
- Create a new folder on the desktop named “Sec4_Ch20”.
- Now open a new notepad++ file and save it as “movies.xml” in the newly created “Sec4_Ch20” folder.
- Write the following code in the file “movies.xml”.
- 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:
- After creating the xml file we would like to validate it. For this we can use xml validators.
- Open your browser and search for xml validator.
- W3schools has xml validator. You will get a hyperlink “XML Validator – W3Schools”, click on it.
- 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.
- Copy the whole code from our “movies.xml” file.
- Replace the code in the block of code of xml validator with the copied xml code.
- Now click on “Validate” button.
- As there is no error in the code, a popup box will appear with a message “No errors found”.
- 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.
- 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.
- Type the format that we need in order to validate each element in movies.dtd file. Syntax for validating an element is as follows:
<director>Francis Ford Coppola</director>
<title>Saving Private Ryan</title>
The XML file can be styled using CSS also.
The output is shown below:
The output is shown below:
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.
<!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:
- In the above code, the statements in
tags are the xml statements,1<note>--</note>being the root element.1<note></note>
- 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).1<!ELEMENT note (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.
<!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>
<!ELEMENT movies (movie)>
<!ELEMENT movie (title,year,genre,director)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT genre (#PCDATA)>
<!ELEMENT director (#PCDATA)>
<!DOCTYPE movies SYSTEM “movies.dtd”
Thus we declared the external DTD file in the xml file.
Thus we created our XML with DTD Schema.