Step by Step: How to Create an XSD File to Validate Data in XML?

Efe Buyuk
3 min readNov 3, 2020
Photo by Emily Morter on Unsplash

In previous section, we mentioned about what is an XSD file and how do we use it in data validation, specifically in XMLs. If you haven’t read it, you can go and read that section before you continue from here. Here is the link of the previous section.

In this section, we are going to see how we can create an XSD file in order to validate data in XML. We will go step by step.

First, we need to create an XSD file to check our XML file. Remember that your XSD file’s file extension should be “.xsd”. I already created an XSD file for you to show the details on it. Here you can see it;

How to create an XSD file

In this XSD file, there are important check rules for our XML file. Let’s look each of them.

1- <xs:element ref=”EmployeeRow” minOccurs=”0" maxOccurs=”unbounded”/>

Here we say that EmployeeRow can have zero occurrence as minimum and there is no limit for maximum. We can have zero record, or we can have unlimited records in our data set.

2- <xs:restriction base=”xs:string”>

In some lines in above XSD file as you can see, there are these lines which says that there is a restriction about being a string for values. When we write this type of a restriction, XSD checks whether the related value is string or not. If a value is not a string data type, then you will get a message about it.

3- <xs:pattern value=”\d{2}[/]\d{2}[/]\d{4}”></xs:pattern>

When we enter a pattern value, then we expect a value as specified in the pattern. For example in this example, we are saying that we are expecting date values as dd/mm/yyyy format. If a value is in this format like 01–01–2020, then it is wrong. It should be like this one: 01/01/2020.

4- <xs:enumeration value=”GBP” />

<xs:enumeration value=”EUR” />

<xs:enumeration value=”USD” />

These restrictions tells that in the specified column, values can have one of these three values and cannot have any other value.

Finally, in order to see our XSD file running, we need to open our both XML and XSD files in Notepad++ and use XML Tools plugin. If you haven’t Notepad++, please download it from here. After you download and install it, you need to install XML Tools plugin. In Notepad++, go to Plugins menu and find Plugins Admin. After you open Plugins Admin, you can search for XML Tools plugin and install it.

I assume that you did all the things as mentioned above paragraph. After that, you can open your both XML and XSD files in Notepad++. When your XML file is active on Notepad++, go to Plugins menu and find XML Tools sub menu under it. Once you hover XML Tools sub menu, you will see its functions listed. As the last step, click Validate now. If you did all steps correctly until now, then you will see a similar message like this;

XSD Validation Check

Note: You can also check this blog post and some other interesting topics on my personal web page.

Next: What Is VBA? How Do We Use It in Data Validation?

--

--

Efe Buyuk

Your data is costing you money. I show you how to reverse that. Follow me on LinkedIn http://tiny.cc/qb07tz