Chapter 2. Constraints

It’s rare that you’ll be able to author XML without worrying about anyone else modifying your document, or anyone having to interpret the meaning of the document. The majority of the time, someone (or something) will have to figure out what your tags mean, what data is allowed within those tags, and how your document is structured. This is where constraint models come into play in the XML world. A constraint model defines the structure of your document and, to some degree, the data allowed within that structure.

In fact, if you take XML as being a data representation, you really can’t divorce a document (often called an instance) from its constraints (the schema). The instance document contains the data, and the schema gives form to that data. You can’t have one without the other; at least, not without introducing tremendous room for error. An instance document without a schema must be interpreted by the recipient; and do you really want him deciding what your elements and attributes meant?

Tip

There’s an argument that essentially goes like this: “Good XML should be structured so that it’s self-documenting.” That’s a good goal, but practically impossible. As a programmer, I often think my code is well documented and easily understood; but I’m assuming a certain level of expertise, and a certain approach to coding. Change just a few bits here and there, and someone else might reasonably interpret my “well-documented” code (or XML) completely differently than ...

Get Java and XML, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.