If, you are using DTD (Document Definition Type) to describe your XML data, then you are still living in the stone age (, unless you’re maintaining existing legacy system and too afraid or lazy to change them, then that is understandable :p ). Instead, use an XML Schema Language which is widely adopted nowadays. There are lots of XML Schema Languages, but 2 most prominent families would be W3C and RELAX NG XML Schemas. I’m kinda guy who just like to stick with the bigger herd. Being a follower instead of an early-adopter, I just chose W3C XML Schema.
Composing XML Schema Descriptor (XSD) is not all dandy. But I’ve came across a utility that can give you a good kick start. It’s an open source utility from my neighboring country, Thailand, and it’s called Trang, which is named after a place in Thailand. You just need to download the jar file.
Let’s get started. Usually, when you’re building a web service, you would need to compose your WSDL file, which in turn, contains the request and response XML schema. Let say, below is my request and response examples:

Sample of a request (DhydratedRequest.xml).

Sample of a response (DhydratedResponse.xml).
Then, run Trang to generate your XSD as below:
![]()
The last 3 arguments were the request xml sample , response xml sample and the name of your-to-be XSD file. Once you run the command you will get below XSD file (DhydratedTypes.xsd):

As you may notice, the generated XSD is not all perfect yet. You may need to make some amendment to make it more suitable for your needs. I will make some changes on the part that I highlighted with red outlined box. For those elements, I want to restrict them with limited acceptable values. The final XSD will looks like below:

Above, is how you can restrict the values of an element. For example, above, TypeOfWater element can only contains either Spring, Tap, Rain or ReverseOsmosis value. Now, you can use above XSD to validate against your request and response XML.
In summary, Trang doesn’t really helps us to compose the perfect XSD, but it helps by giving us a good kick start, especially, for an XSD newbie like me :) . Plus, it will be less error-prone if you start by generating your schema. Although, above sample request and response is quite simple, the XSD contains lots of repetitive keywords and prefixes which a typing error may be failed to be seen by our naked eye. So, play safe and generate!