An XML document that is well created can be validated using DTD (Document Type Definition) or XSD (XML Schema Definition). A well-formed XML document should have correct syntax and should follow the below rules:

  • It must start with the XML declaration.
  • It must have one unique root element enclosing all the other tags.
  • All start tags must have end tags.
  • XML tags are case-sensitive.
  • All elements must be closed with the proper ending tag.
  • All elements must be nested properly.
  • All attributes’ values must be in quotes.
  • For special characters, XML entities must be used.
  • It must start with the XML declaration.
  • It must have one unique root element enclosing all the other tags.
  • All start tags must have end tags.
  • XML tags are case-sensitive.
  • All elements must be closed with the proper ending tag.
  • All elements must be nested properly.
  • All attributes’ values must be in quotes.
  • For special characters, XML entities must be used.

XML DTD:

DTD defines the structure of the document containing a list of all the legal elements and attributes. The main motive of the DTD is to define the structure of an XML document.

We must avoid errors in the XML documents because any error will stop the execution of XML programs

Example:

Step 1. Create an XML file:

<?xml version="1.0"?>
  <!DOCTYPE list SYSTEM "simple_recipe.dtd">
  <list>
  <recipe>
    <author>ABC</author>
    <recipe_name>Chocolate Chip Bars</recipe_name>
    <meal>Dessert</meal>
    <ingredients>
      <item>2/3 Cup butter</item>      <item>2 Cup brown sugar</item>
      <item>1 tsp vanilla</item>     <item>1 3/4 Cup all-purpose flour</item>
      <item>1 tsp baking powder</item>
      <item>pinch of salt</item>      <item>2 eggs</item>
      <item>1/2 Cup chopped nuts</item>
      <item>2 cups (12-oz pkg.) semi-sweet choc. chips</item>
    </ingredients>
    <directions>
 Preheat oven to 350 degrees. 
 Melt the butter; add brown sugar and vanilla in a large mixing bowl. 
 Set aside to cool it down. Mix the all-purpose flour, baking powder, and salt and keep it aside. 
 Add eggs to the cooled sugar mixture and beat well. 
 Stir and add dry ingredients, nuts, and chips. 
 Grease a 13-by-9-inch pan with butter.
 Bake for 30 to 40 minutes until it turns golden brown and then waits for it cool down. After it is completely cool, cut into square pieces and serve.
    </directions>
  </recipe>
 </list>

Step 2. Create a DTD file:

<!ELEMENT list (recipe+)>  
<!ELEMENT recipe (author, recipe_name, meal, ingredients, directions)> 
<!ELEMENT author (#PCDATA)>  
<!ELEMENT recipe_name (#PCDATA)>  
<!ELEMENT meal (#PCDATA)>  
<!ELEMENT ingredients (item+)>  
<!ELEMENT item (#PCDATA)>  
<!ELEMENT directions (#PCDATA)> 

XML XSD:

XSD is used as an alternative to DTD. XSD is also used to create a well formed XML document. To reuse the existing definitions namespaces are used in XSD.

Example:

Step 1. Create an XML file:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <records>
        <record>
            <title>Brand New Eyes</title>
            <artist>Paramore</artist>
            <genre>Punk Rock</genre>
            <year>2011</year>
        </record>
        <record>
            <artist>Various Artist</artist>
            <genre>Rock</genre>
            <year/>
        </record>
    </records>
</root>

Step 2. Create an XSD file:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified"
           elementFormDefault="qualified">
    <xs:element name="root" type="rootType">
    </xs:element>

    <xs:complexType name="rootType">
        <xs:sequence>
            <xs:element name="records" type="recordsType"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="recordsType">
        <xs:sequence>
            <xs:element name="record" type="recordType" maxOccurs="unbounded" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="recordType">
        <xs:sequence>
            <xs:element type="xs:string" name="title"/>
            <xs:element type="xs:string" name="artist"/>
            <xs:element type="xs:string" name="genre"/>
            <xs:element type="xs:short" name="year"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

Step 3. Create a JAVA code:

import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Objects;

public class XMLValidator {
    public static final String XML_FILE = "records.xml";
    public static final String SCHEMA_FILE = "records.xsd";

    public static void main(String[] args) {
        XMLValidator XMLValidator = new XMLValidator();
        boolean valid = XMLValidator.validate(XML_FILE, SCHEMA_FILE);

        System.out.printf("%s validation = %b.", XML_FILE, valid);
    }

    private boolean validate(String xmlFile, String schemaFile) {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        try {
            Schema schema = schemaFactory.newSchema(new File(getResource(schemaFile)));

            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(new File(getResource(xmlFile))));
            return true;
        } catch (SAXException | IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    private String getResource(String filename) throws FileNotFoundException {
        URL resource = getClass().getClassLoader().getResource(filename);
        Objects.requireNonNull(resource);

        return resource.getFile();
    }
}

Difference between DTD and XSD:

No.DTDXSD
1DTD refers to Document Type Definition.XSD refers to XML Schema Definition.
2These are derived from SGML syntax.These are written in XML.
3It does not support datatypes.It supports datatypes.
4It does not support namespaces.It supports namespaces.
5It does not define the order of child elements.It defines the order of child elements.
6It is not extensible.It is extensible.
7It is not simple and easy to learn.It is simple and easy to learn.
8It provides less control over the XML structure.It provides more control over the XML structure.
One thought on “How to Validate XML using XSD/DTD?”
  1. is it possible to validate XML Document by DTD & XSD at the same time? what do i mean is the DTD & XSD validate code is inserted on the XML File. like this

Leave a Reply

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