What problems can happen if an XML file is not backed by a schema?

What is a XML schema in plain simple terms? What will be the problem if an XML doesn't have a schema defined? An example can be very helpful.

• You might try to process arbitrary XML. What will your app do if I send <xml><sillyTag>some random text</sillyTag></xml>? – Dan Pichelman Apr 18 '13 at 19:05
• @Dan: most programs out them will exit with an error. I don't see a big issue here. – Codism Apr 18 '13 at 21:50

The main problem I see is that it cannot be validated either against a DTD, or against an XML Schema. As such, you don't have any guarantee that what you received is what you expected.

For example, if you are sending this XML file to someone:

<x id="2">
<yyy>
<zzz>fdslgskfmlsdk</zzz>
<zzz>sdmflsdmfl</zzz>
</yyy>
<yyy>thingy</yyy>
</x>


How can he be certain that:

1. is "id" always a number ?
2. is "id" mandatory or optional ?
3. is "id" the only attribute of the "x" element ?
4. how many "yyy" elements can be contained in a "x" element ?
5. is "yyy" the only element that can be inside "x" ?
6. how many "zzz" elements in a "yyy" element ?
7. etc.

There is an unlimited list of questions that person can ask to you. The goal of an XML Schema is to hopefully answer all these questions and also to provide a way to validate a document before or during the parsing phase.

Not fond of w3schools, but here is a list of short and interesting reads on XML Schema:

It's the same question as using a static or dynamically-typed language. Schema is a declarative check on the correctness of your data. If you have a field that has to be an integer, you can mark as such in your schema and then invalid data won't get past the validator. Without a schema, you would have to check in your code which is more error-prone. Sometimes, you might have a preference for flexibility in which case a schema is a hindrance. Notably there is no standard schema language for JSON, yet it is growing in popularity.