EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials XML Tutorial XML Validation
Secondary Sidebar
XML Tutorial
  • Basic
    • What is XML?
    • XML Tags
    • XML URL
    • XPath Sibling
    • XML root element
    • XML Encryption
    • XML Parsing php
    • xml parsing with java
    • Dataset XML
    • XML Parser in C#
    • XML Tree
    • XML boolean
    • XML sitemap
    • XML Array
    • XML reserved characters
    • XML Viewer
    • XML Uses
    • XML Special Characters
    • XML generator
    • XML file format
    • XML DOM
    • XML ampersand
    • XML Mapping
    • XML File
    • XML Element
    • XML HttpRequest
    • XML XSD
    • XML Schema
    • XML Namespaces
    • XML Comments
    • XML Attributes
    • XML Encoding
    • XML Validation
    • XML CDATA
    • XML Database
    • XML Technologies
    • XML Error
    • XML DTD
    • XML Date
    • XML Parsers
    • String in XML
    • XML with CSS
    • XML Versions
    • XML Features
    • XML Commands
    • Servlet web.xml
    • XPath Injection
    • XPath Functions
    • XPath starts-with
    • XPath Selector
    • XPath Count
    • XPath XML
    • XML Parsing in Oracle
    • XML parsing in python
  • Xpath
    • What is XPath?
    • XPath namespace
    • XPath for JSON
    • XPath Last Element
    • Xpath Substring
    • XPath First Element
    • XPath local-name
    • XPath Multiple Attributes
    • XPath Matches
    • XPath Position
    • XPath Index
    • XPath regex
    • XPath id contains
    • XPath innertext
    • XPath Multiple Conditions
    • XPath Helper
    • XPath Generator
    • XPath ID
    • XPath Locator
  • Xquery
    • What is Xquery

XML Validation

By Priya PedamkarPriya Pedamkar

XML Validation

Introduction to XML Validation

XML validation is a process done to check for a syntax error in an XML document to ensure that the document is well-written with the standard rules using either DTD or schema. A complete XML file is considered to be valid XML document unless it has correct syntax and constraints defined in it. To check for the validation document type definitions follow two schemas and it is checked for well-doing so no need to consider the data and structure of the XML document. For instance, if the data is the location and the first name, validation works if the respective values are provided without non-empty. Validation is preferred to make sure that XML file is sent between a client and the server which does the service of web service that is acknowledged correctly.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

1. For DTD to validate the code

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT name (child elements, *)>
<!ELEMENT
<!ATTLIST
Att name       CDATA   #REQUIRED
Att name       CDATA   #IMPLIED
>

2. For XSD

<?xml version="1.0" encoding="UTF-8">
<xsd:schemaxmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsd:element name="">
<xsd:complexType>
<xsd:sequence>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

How does Validation work in XML?

  • Validation Process is dealt with XML Parser in two different ways.
  • One is Well-defined XML Document and another one is Valid XML Document.
  • Validation is optional but when it is about data integrity it is highly recommended.
  • In most of the cases, validation of XML is done through schemas, not by DTD.
  • The XML parser plays the main role in Validation has two categories like validating and non-validating.
  • Former validate against DTD and later doesn’t validate XML with respective to DTD.

Associating XML Document with DTD

XML document done with correct syntax is termed to be well-formed and it should be validated appropriately with Document Type Definition (DTD.) It checks the vocabulary and grammatic rules. Applications that make use of XML documents should match the data with the corresponding definitions and validation lies on post-processing. Validation can be performed either using Validation tools like IDE and through on-line validators.

Validating XML file with DTD requires:

  • XML File
  • DTD document where DTD is a collection of elements and attributes.

Once the above two processes are done it is required to parse a xml file using function parser () which automatically generates Document Object Tree using DOM API in java. This parser reports warning messages at the end of the validation processing. Next is to enabling setvalidating () method to “TRUE”. We can also validate the file using NetBean. Since DTS doesn’t support Namespaces a basic drawback for XML DTD validation. To overcome this XML it has an advantage of using Schema Validation.

Associating XML Document with XML Schema

XML Schema is also termed as XML Schema Definition defines a newer way to validate XML fragment. Schemas are necessary when transferring the structure of the XML document to a machine. Validating means running a process to ensure that the XML Document proceeds the rules defined by the standard schemas.

Speaking, schemas are validated due to data completeness of checking the required information, a data structure of the elements and attributes are corrected like the order of child elements. Also, there are some tools published by the industry to perform these great checks. Even though schemas are validated in some cases it is not as simple. In some cases it may pass validation but leaving a warning message.

Examples of XML Validation

Given below are the examples mentioned:

Example #1

Here we are going to write XML file for Hospital and save the file as dr.xml.

Code:

dr.xml

<?xml version = "1.0" ?>
<!DOCTYPE Hospital SYSTEM "Hospital.dtd">
<Doctor>
<D_Id> D-003 </D_Id>
<D_Name> Joy Angeline </D_Name>
<Gender> Female </Gender>
<E_ID> [email protected] </E_ID>
</Doctor>

Next, DTD file is generated for the above XML file where the document starts with <[ ]>.

Hospital.dtd

<!ELEMENT Doctor (D_Id, D_Name,Gender,E_ID)>
<!ELEMENT D_Id (#PCDATA)>
<!ELEMENT D_Name (#PCDATA)>
<!ELEMENT Gender (#PCDATA)>
<!ELEMENT E_ID (#PCDATA)>

If the validation is not successfully done it is necessary to modify the XML document to make well-formed XML Document.

Note: In this example, we have used Notepad++ for dtd validation. Before executing follow the steps to plugins xml tool. Go to notepad++ -> click plugins ->Plugin Manager -> generate XML tool (automatically updates in the list)and click ok button. Then in the toolbar, you could see plugins-> XML tools, right-click and click validate now. The DTD is validated for the corresponding xml file. (create dr.xml and Hospital.dtd in the notepad++) then click xml file and follow the above procedure to check for validation.

Output:

Plugin Installation

plugin installation

After Validating

xml validation 2

Example #2

Using Java XML file and XSD are validated.

Code:

ps.xml

<?xml version = "1.0"?>
<grade>
<stud id = "435">
<firstname>joseph</firstname>
<lastname>raj</lastname>
<score>75</score>
</stud>
<stud id = "355">
<firstname>Vani</firstname>
<lastname>Neelakandan</lastname>
<score>95</score>
</stud>
<stud id= "555">
<firstname>Vinothalin</firstname>
<lastname>Stefen</lastname>
<score>88</score>
</stud>
</grade

sc.xsd

<xs:schemaattributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="grade">
<xs:complexType><xs:sequence><xs:element name="stud" maxOccurs="unbounded" minOccurs="0">
<xs:complexType><xs:sequence>
<xs:element type="xs:string" name="firstname"/>
<xs:element type="xs:string" name="lastname"/>
<xs:element type="xs:byte" name="score"/>
</xs:sequence>
<xs:attribute type="xs:short" name="id" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element></xs:schema>

Xmlv.java

import java.io.File;
import java.io.IOException;
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 org.xml.sax.SAXException;
public class Xmlv {
public static void main(String[] args) {
if(args.length !=2){
System.out.println( "XSDValidator<fname.xsd><fname.xml>" );
} else {
booleanisV = validateXMLSchema(args[0],args[1]);
if(isV){
System.out.println(args[1] + " is well-formed " + args[0]);
} else {
System.out.println(args[1] + " is not well-formed  " + args[0]);
}
}
}
public static booleanvalidateXMLSchema(String xsdP, String xmlP){
try {
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema s = factory.newSchema(new File(xsdP));
Validator validator = s.newValidator();
validator.validate(new StreamSource(new File(xmlP)));
} catch (IOException e1){
System.out.println("Exception: "+e1.getMessage());
return false;
}catch(SAXException e2){
System.out.println("SAX Exception: "+e2.getMessage());
return false;
}
return true;
}
}

Save all the three files in the same folder and execute in javajdk. (example e:\java\javac file name).

Save all the three files in the same folder

Output:

xml validation 4JPG

Advantages of using Validation in XML

Given below are the advantages:

  • If no validation the respective files cannot be viewed clearly. When using validation mismatches or missing information between the datasets can be viewed clearly.
  • Does consistency and completeness.
  • XML schema validation is preferred more than DTD validation probably the reason is the schema defines rules on the content and the document of XSD are themselves doe in XML which is unlike DTD grammar.
  • Using XML Parser and XML Dom to resolve the error using validation.
  • Also, it helps in matching the namespaces of a compiled schemas along with xmlns namespaces.

Conclusion

Therefore, in this article, we have seen xml validation mechanism and also saw XML DTD structures and schemas. It is quite difficult if the XML validation is big. Having validating using XML it is advisable to look on specific values and data relationships schema validation tool present only consistent results, therefore it is suggested to use validated tools. Choosing to use either DTD or schemas is always depending on what you do with XML. Some new technologies, especially web services,depending on the schemas.

Recommended Articles

This is a guide to XML Validation. Here we discuss the introduction to XML Validation, along with how does validation work, associating an XML document with DTD and advantages. You may also have a look at the following articles to learn more –

  1. XML Features
  2. XML in Excel
  3. XML Versions
  4. XML with CSS
Popular Course in this category
XML Training (5 Courses, 6+ Projects)
  5 Online Courses |  6 Hands-n Projects |  40+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more