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:
<?xml version="1.0"?> <movies> <movie> <title>The Godfather</title> <year>1972</year> <genre>Drama</genre> <director>Francis Ford Coppola</director> </movie> <movie> <title>Superbad</title> <year>2007</year> <genre>Comedy</genre> <director>Greg Mottola</director> </movie> <movie> <title>The Departed</title> <year>2006</year> <genre>Drama</genre> <director>Martin Scorsese</director> </movie> <movie> <title>Saving Private Ryan</title> <year>1998</year> <genre>Action</genre> <director>Steven Spielberg</director> </movie> <movie> <title>The Expendables</title> <year>2010</year> <genre>Action</genre> <director>Sylvester Stallone</director> </movie> </movies>
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:
<?xml version="1.0"?> <!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <message>Don't forget me this weekend!</message> </note>
tags are the xml statements,
being the root element.
are DTD schema statements validating xml part of the file.
contains child elements in it like to, from, heading, etc.
<!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).
<!DOCTYPE movies[ <!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.