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:
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.
Output:
Plugin Installation
After Validating
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).
Output:
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 –