Software Testing Class

What is XML Schema Inference?

Schema Inference is a technique which is used to infer XSD (XML Schema Definition) after parsing the structure of any XML document. Therefore using a programming language like (.NET framework), one can generate the XSD from the structure of XML document and can use it to validate the same original XML document.

You may like article: SoapUI Tutorial Series

For example in .NET framework, class known as c present at the namespace of System.Xml.Schema can be used to generate XSD from the XML document which can be used to validate the original document. Also, this class infers the schema components in a constrained way after analyzing the most restrictive type of a particular attribute or element in the XML document. After gathering more and more information from the XML document, it loosens the constraints by inferring element or attribute which is least restrictive type. For example, xs:string is the least restrictive type that can be inferred from the XML document.

XML Schema Inference
xml schema inference

Let’s understand this through a piece of XML document as follows:

In the above example,

XmlSchemaInference class will throw an exception XmlSchemaInferenceExceptionif it encounters an inline XML Schema definition language (XSD) schema.

Rules to infer schema node type and structure:

During schema inference there are set of rules that are obeyed while inferring nodes from XML document to XSD schema. Here are going to understand how the rules work. There are eight types of possible structures as given below on which these rules works.

Rules to Infer Elements with Simple Type:

Rules to infer simple types:our XmlSchemaInference class as discussed can infer the datatype for the attributes of the element while parsing the XML document a sit encounters with it. If attribute value is positive number it infers it as unsigned Byte, it is an alphabet then it infer its datatype as string and so on. Following are the list of possible datatypes that the rules can infer datatype of attributes of elements from XML document to XSD schema.

Inferred Data type (Simple Type)Attributes value for XML element
booleanIf it is true or false.
byteIf it is integer value between –128 to 127.
unsignedByteIf it is integer value between 0 to 255.
shortIf it is integer value between –32768 to 32767.
unsignedShortIf it is integer value between 0 to 65535.
intIf it is integer value between –2147483648 to 2147483647.
unsignedIntIf it is integer value between 0 to 4294967295.
longIf it is integer value between –9223372036854775808 to 9223372036854775807.
unsignedLongIf it is integer value between 0 to 18446744073709551615.
integerAny finite integer which is prefixed with “-”.
floatIf it is decimal value between -16777216 to 16777216.
doubleIf it is decimal value between -9007199254740992 to 9007199254740992.
durationW3C duration format.
dateTime & timeW3C dateTime and time format.
Date W3C date format.
stringIf it is single or more than one Unicode format.

JAVA mini Project:

Till now we have discussed about what Schema Inference is and how it is used in .NET to generate XSD from the XML document to validate the original document. Likewise .NET framework, it can also be achieved in JAVA Platform using xsd-gen-0.2.0-jar-with-dependencies.jar or xbean-2.2.0.jar.

JAVA Project using xsd-gen-0.2.0-jar-with-dependencies.jarto infer XSD from XML document.

Step 1: Download the jars (xsd-gen-0.2.0-jar-with-dependencies.jar) from this website at below link.

http://www.java2s.com/Code/Jar/x/Downloadxsdgen020jarwithdependenciesjar.htm

Step 2: Download the project from below link and set it up in your eclipse.

Step 3: Set up the build path by importing the external jars (xsd-gen-0.2.0-jar-with-dependencies.jar) at build path of the project.

Step 4:Once compilations is complete.

Step 5: Place the file at location C:\schemainference\sample.xml, this file can be downloaded from below link.

Step 6: Run the project main class SchemaInference (shown below) as Java application in eclipse.


package com.softwaretestingclass.kanif.xml.schema;

import java.io.File;

import java.io.FileOutputStream;

publicclass SchemaInference {

publicstaticvoid main(String[] args) throws Exception {

File file = new File("C://schemainference//sample.xml");

XsdGen gen = new XsdGen();

System.out.println(gen.parse(file).toString());

File out = new File("C://schemainference//out.xsd");

gen.write(new FileOutputStream(out));
}
}

Step 7: Output will be visible on the console as generated XSD from XML document .

Step 8: Output XSD file as out.xsd will be generated at “C:\schemainference\out.xsd” path which is nothing but our inferred schema.

Input XML:This is the screenshot of the contents of sample.xml document.

Output:This is the equivalent XSD inferred from the XML document using the above project. Best part in this is that this XSD can be used to validate this XML document.

Application of Schema Inference in SOAP XML Response:

It is the simple method used to generate the XSD by Schema Inference of SOAP XML response message from web service.

Response SOAP XML message is given below. If we like to know the XSD of this response message, it is very simple and can be obtained using this mini project in JAVA.

Attached below is the screenshot for inferred XSD schema from SOAP XML response message which we tested with SoapUI in this tutorial.

This completes the tutorial on Schema Inference. Hope you find the required information.

Conclusion:

By any chance if you you worked on XML Schema Interface then please let us know your feedback and comments. Your questions are most welcome!


⇓ Subscribe Us ⇓


If you are not regular reader of this website then highly recommends you to Sign up for our free email newsletter!! Sign up just providing your email address below:


 

Check email in your inbox for confirmation to get latest updates Software Testing for free.


  Happy Testing!!!
 
Exit mobile version