All IT Courses 50% Off
JAVA Tutorials

How to Validate XML using XSD/DTD?

Table of Contents

Validate XML Using XSD/DTD

To validate XML, you can use either an XSD (XML Schema Definition) or a DTD (Document Type Definition).

XML (Extensible Markup Language) is a widely used format for data representation and exchange. Ensuring the correctness and structure of XML documents is essential, and this is where validation using XSD (XML Schema Definition) or DTD (Document Type Definition) comes into play. This guide will walk you through the process of validating XML using XSD and DTD.

XML validation is a crucial step in ensuring that your XML documents adhere to a predefined structure and set of rules. This not only ensures data integrity but also facilitates smooth data exchange between different systems. There are two primary methods for validating XML documents:

  1. XSD (XML Schema Definition)
  2. DTD (Document Type Definition)

Validating XML with XSD

XSD is more powerful and expressive than DTD. It allows for precise definition of data types, element structures, and constraints.

All IT Courses 50% Off

Step-by-Step Guide:

Create an XSD File:

Define the structure, elements, and data types for your XML document.

   <?xml version="1.0" encoding="UTF-8"?>
   <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
       <xs:element name="note">
           <xs:complexType>
               <xs:sequence>
                   <xs:element name="to" type="xs:string"/>
                   <xs:element name="from" type="xs:string"/>
                   <xs:element name="heading" type="xs:string"/>
                   <xs:element name="body" type="xs:string"/>
               </xs:sequence>
           </xs:complexType>
       </xs:element>
   </xs:schema>

Create an XML File:

Write an XML document that you want to validate.

   <?xml version="1.0" encoding="UTF-8"?>
   <note xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="note.xsd">
       <to>Tove</to>
       <from>Jani</from>
       <heading>Reminder</heading>
       <body>Don't forget me this weekend!</body>
   </note>

Validate the XML Document:

Use a parser that supports XSD validation. Below is an example using Python and the lxml library.

   from lxml import etree

   xml_file = 'note.xml'
   xsd_file = 'note.xsd'

   xml_doc = etree.parse(xml_file)
   xml_schema_doc = etree.parse(xsd_file)
   xml_schema = etree.XMLSchema(xml_schema_doc)

   is_valid = xml_schema.validate(xml_doc)
   print(f"XML is valid: {is_valid}")

Using XSD:

  • Create an XSD file that defines the structure and data types of your XML.
  • Reference the XSD file in your XML using the schemaLocation attribute.
  • Use an XML parser that supports XSD validation to check the XML against the schema.

Validating XML with DTD

DTD is simpler and defines the structure of an XML document through a list of legal elements and attributes.

Step-by-Step Guide:

Create a DTD File:

Define the structure and allowed elements for your XML document.

       <!ELEMENT note (to, from, heading, body)>
       <!ELEMENT to (#PCDATA)>
       <!ELEMENT from (#PCDATA)>
       <!ELEMENT heading (#PCDATA)>
       <!ELEMENT body (#PCDATA)>

    Create an XML File:

    Include a reference to the DTD in your XML document.

         <?xml version="1.0" encoding="UTF-8"?>
         <!DOCTYPE note SYSTEM "note.dtd">
         <note>
             <to>Tove</to>
             <from>Jani</from>
             <heading>Reminder</heading>
             <body>Don't forget me this weekend!</body>
         </note>

      Validate the XML Document:

      Use a parser that supports DTD validation. Below is an example using Python and the lxml library.

           from lxml import etree
        
           xml_file = 'note.xml'
        
           xml_doc = etree.parse(xml_file)
           dtd_valid = xml_doc.docinfo.internalDTD.validate(xml_doc)
           print(f"XML is valid: {dtd_valid}")

        Using DTD:

        • Create a DTD file or include the DTD inline within your XML file.
        • Reference the DTD in your XML using the DOCTYPE declaration.
        • Use an XML parser that supports DTD validation to verify the XML.

          Validating XML ensures it conforms to the defined structure, improving data integrity and consistency.

          Requirements

          • XML editor or IDE
          • Basic knowledge of XML and schema languages (DTD, XDR, XSD)

          Create an XML file Document

          • Write an XML file that needs validation.

          Create a DTD and Link to the XML Document

          • Develop a DTD file defining the structure.
          • Link the DTD to your XML using a DOCTYPE declaration.

          Perform Validation Using a DTD

          • Use an XML parser or validation tool to validate the XML against the DTD.

          Create an XDR Schema and Link to the XML Document

          • Write an XDR schema outlining the XML structure.
          • Reference the XDR schema in your XML file.

          Perform Validation Using an XDR Schema

          • Validate the XML document with an XML validator that supports XDR.

          Create an XSD Schema and Link to the XML Document

          • Design an XSD schema to define the XML structure and data types.
          • Link the XSD schema in your XML file using the xsi:schemaLocation attribute.

          Perform Validation Using an XSD Schema

          • Validate the XML file against the XSD using an XML schema validator.

          Use Namespaces in the XSD Schema

          validate xml
          • Incorporate XML namespaces to avoid element name conflicts in the schema.

          Cache Namespaces

          • Optimize validation performance by caching namespaces.

          Verification

          • Ensure the XML document is correctly validated and adheres to the specified schema rules.

          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.

          validate xml

          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.

          validate xml

          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.

          Insert XML Documents into XML Typed Columns

          When working with XML data in databases, you can store XML documents in XML-typed columns to leverage the structured nature of XML. Here’s how you can insert XML documents into XML-typed columns:

          Define the XML-typed Column:

          Ensure that your table has a column defined to store XML data.

               CREATE TABLE MyTable (
                   ID INT PRIMARY KEY,
                   XMLData XML
               );

            Insert XML Data:

            Use the INSERT statement to add XML documents to the XML-typed column.

                 INSERT INTO MyTable (ID, XMLData)
                 VALUES (1, '<Person><Name>John Doe</Name><Age>30</Age></Person>');

              By defining a column as XML type, you enable the storage of well-formed XML documents within the database, ensuring data integrity and providing flexibility for querying and manipulation.

              Update XML Documents Stored in an XML Column

              Updating XML documents stored in an XML column allows you to modify the content of the XML data directly within the database. Here’s how to update XML documents:

              Use the UPDATE Statement with modify():

              The modify() method is used to make changes to the XML content.

                   UPDATE MyTable
                   SET XMLData.modify('replace value of (/Person/Name/text())[1] with "Jane Doe"')
                   WHERE ID = 1;

                Update Multiple Nodes:

                You can also update multiple nodes within the XML document.

                     UPDATE MyTable
                     SET XMLData.modify('replace value of (/Person/Age/text())[1] with "31"')
                     WHERE ID = 1;

                  Using the modify() method allows for precise updates to specific parts of the XML document, maintaining the structure and consistency of the stored data.

                  Delete Rows Based on the Content of XML Documents

                  Deleting rows based on the content of XML documents involves querying the XML data to identify rows that meet specific criteria and then removing them. Here’s how to delete such rows:

                  Querying the XML Content:

                  Use the exist() method to filter rows based on XML content.

                       DELETE FROM MyTable
                       WHERE XMLData.exist('/Person[Name="John Doe"]') = 1;

                    Complex Conditions:

                    You can also apply more complex conditions to identify rows to delete.

                         DELETE FROM MyTable
                         WHERE XMLData.exist('/Person[Age>30]') = 1;

                      By leveraging the exist() method, you can efficiently delete rows that match specific conditions within the XML documents, ensuring that the database remains clean and relevant.

                      Query XML Data

                      Querying XML data stored in XML-typed columns allows you to extract and manipulate the data as needed. Here’s how to query XML data:

                      Extract Data Using value():

                      The value() method can be used to extract specific values from the XML document.

                           SELECT XMLData.value('(/Person/Name/text())[1]', 'VARCHAR(100)') AS Name
                           FROM MyTable;

                        Using nodes() Method for Multiple Values:

                        The nodes() method allows you to handle multiple XML nodes.

                             SELECT T.N.value('(Name/text())[1]', 'VARCHAR(100)') AS Name,
                                    T.N.value('(Age/text())[1]', 'INT') AS Age
                             FROM MyTable
                             CROSS APPLY XMLData.nodes('/Person') AS T(N);

                          Combining Methods for Complex Queries:

                          Combine value(), nodes(), and other methods to perform complex queries.

                               SELECT T.N.value('(Name/text())[1]', 'VARCHAR(100)') AS Name,
                                      T.N.value('(Age/text())[1]', 'INT') AS Age
                               FROM MyTable
                               CROSS APPLY XMLData.nodes('/Person[Age > 25]') AS T(N);

                            Querying XML data with these methods allows for flexible and powerful data extraction and manipulation, enabling you to make the most of your XML-typed columns in the database.

                            XMLVALIDATE Scalar Function

                            The XMLVALIDATE scalar function is a powerful feature used in various database management systems to validate XML data against a specified XML schema. This function ensures that the XML data conforms to the defined structure and rules outlined in the schema, enhancing data integrity and reliability. Here’s a detailed explanation of the XMLVALIDATE scalar function, its usage, and its benefits.

                            Overview

                            The XMLVALIDATE scalar function takes an XML document as input and validates it against an XML schema. If the XML document conforms to the schema, the function returns the validated XML data. If the document does not conform, an error is raised, indicating the nature of the validation failure.

                            Syntax

                            The basic syntax of the XMLVALIDATE scalar function is as follows:

                            XMLVALIDATE (XML_data_expression ACCORDING TO XMLSCHEMA schema_name)
                            • XML_data_expression: This is the XML data that needs to be validated.
                            • schema_name: This is the name of the XML schema against which the XML data is validated.

                            Usage

                            The XMLVALIDATE function is typically used in SQL queries to ensure that the XML data stored or being inserted into the database complies with the predefined schema. Here are some common scenarios where XMLVALIDATE is useful:

                            1. Data Insertion:
                              When inserting XML data into a database, XMLVALIDATE can be used to validate the data before the insertion to ensure that only valid XML documents are stored.
                               INSERT INTO xml_table (xml_column)
                               VALUES (XMLVALIDATE(? ACCORDING TO XMLSCHEMA 'schema_name'));
                            1. Data Retrieval:
                              When retrieving XML data, XMLVALIDATE can be used to validate the data on the fly, ensuring that the retrieved data is schema-compliant.
                               SELECT XMLVALIDATE(xml_column ACCORDING TO XMLSCHEMA 'schema_name')
                               FROM xml_table;
                            1. Data Update:
                              When updating XML data, XMLVALIDATE ensures that the updated data remains valid according to the schema.
                               UPDATE xml_table
                               SET xml_column = XMLVALIDATE(? ACCORDING TO XMLSCHEMA 'schema_name')
                               WHERE id = ?;

                            Benefits

                            Using the XMLVALIDATE scalar function provides several benefits:

                            1. Data Integrity:
                              By validating XML data against a schema, XMLVALIDATE ensures that the data adheres to the defined structure and rules, preventing invalid or malformed XML documents from being stored in the database.
                            2. Error Prevention:
                              Early validation of XML data helps identify and correct errors before they propagate through the system, reducing the risk of data corruption and improving overall data quality.
                            3. Compliance:
                              For applications that require strict adherence to data standards, such as financial or healthcare applications, XMLVALIDATE ensures that all XML data complies with the relevant standards and regulations.
                            4. Enhanced Querying:
                              Validated XML data can be more efficiently queried and processed, as it conforms to a known structure, enabling better performance and more accurate query results.

                            Example

                            Here’s a practical example of using the XMLVALIDATE scalar function:

                            -- Define a table with an XML column
                            CREATE TABLE orders (
                                order_id INT PRIMARY KEY,
                                order_details XML
                            );
                            
                            -- Insert valid XML data
                            INSERT INTO orders (order_id, order_details)
                            VALUES (1, XMLVALIDATE('<order><id>1</id><item>Book</item></order>' ACCORDING TO XMLSCHEMA 'order_schema'));
                            
                            -- Attempt to insert invalid XML data
                            INSERT INTO orders (order_id, order_details)
                            VALUES (2, XMLVALIDATE('<order><id>2</id><item></order>' ACCORDING TO XMLSCHEMA 'order_schema'));
                            -- This will raise an error due to invalid XML structure

                            XML Tags are Case-Sensitive

                            XML, or Extensible Markup Language, is a widely-used format for structuring data. One of the critical aspects of XML is that its tags are case-sensitive. This characteristic means that the tags must be used with consistent capitalization throughout the document, as XML differentiates between uppercase and lowercase letters.

                            Importance of Case Sensitivity in XML

                            Data Integrity:

                              • Case sensitivity ensures that data is accurately interpreted by different systems. If tags were not case-sensitive, it could lead to misinterpretation of data and errors in data processing.

                              Consistency:

                                • Enforcing case sensitivity promotes consistency in the structure of XML documents. Consistent use of tags makes the documents easier to read, understand, and maintain.

                                Standards Compliance:

                                  • Many industry standards and protocols that use XML enforce case sensitivity to maintain uniformity and interoperability between systems.

                                  Examples of Case Sensitivity in XML

                                  Consider the following XML snippet:

                                  <Person>
                                      <Name>John Doe</Name>
                                      <Age>30</Age>
                                  </Person>

                                  In this example, the tags <Person>, <Name>, and <Age> are used with specific capitalization. If we change the capitalization, the XML parser would treat them as different tags:

                                  <person>
                                      <name>John Doe</name>
                                      <age>30</age>
                                  </person>

                                  Here, <person>, <name>, and <age> are different from <Person>, <Name>, and <Age>, respectively. This discrepancy can lead to errors in data interpretation or processing.

                                  Best Practices for Using XML Tags

                                  Consistency:

                                    • Always use the same capitalization for tags throughout your XML document. Decide on a convention (e.g., camelCase, PascalCase) and stick to it.

                                    Validation:

                                      • Use XML validation tools to check for consistency and correctness in your XML documents. These tools can help identify case sensitivity issues early.

                                      Documentation:

                                        • Document your XML tag conventions and structure clearly. This documentation helps ensure that everyone working with the XML files follows the same guidelines.
                                        Facebook Comments

                                        One Comment

                                        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 *

                                        This site uses Akismet to reduce spam. Learn how your comment data is processed.

                                        Related Articles

                                        Back to top button