Reputation: 3380
Is that a way to validate XML without schemas?
I am researching the concept to validate XML without using XML Schema, rather to use raw Java to construct the validation. I know about using SAX or DOM parsers to parse the XML file, but I have no idea whether either of these parsers are used validate XML in a manner similar to XML Schema.
If anyone has an idea how to validate XML without using schemas using any tool (Java or any other), kindly share your ideas.
Upvotes: 1
Views: 5197
Reputation: 25034
In general, 'schema' is a word we use to describe a concise statement, usually in machine-processable form, of the rules we would like a collection of data (here, an XML document) to follow; 'validation' is the process of deciding whether a given collection of data does or does not follow those rules.
Validating a document without a schema would be a lot like checking to see whether a car is obeying the speed limit, on a stretch of road for which no maximum speed is prescribed, or serving as a referee in a sport with no rules at all, and thus no need for a referee: not so much impossible as meaningless.
I suppose that in reality by "without a schema" you may mean "with a schema in some language other than XSD" -- sure, you can use DTDs or Relax NG or Schematron -- actually you could use any of a large number of other schema languages, but most of them are now defunct. Or you may mean "without writing down validation rules in any declarative language", e.g. by writing a Java program. Also possible, though less likely to produce reliable results. And as Michael Kay has already suggested, a language like XSLT would allow a somewhat more compact program for most XML vocabularies.
Upvotes: 1
Reputation: 163322
Sure, you can write a program to do custom validation of XML. The question is, why would you want to? Declarative schema languages were invented for a good reason.
If you really do need to enforce validation rules that are difficult to express in a declarative schema language, consider using an XSLT stylesheet: it will be a fraction of the number of lines of code you would write in Java, and much easier to maintain.
Upvotes: 3
Reputation: 111541
Yes, you could validate XML without a schema, but it would be rather unorthodox to do so.
Schemas generically are grammars that declaratively express the acceptability of a language. You could instead write procedural code that checked your XML against whatever criteria you wish. Such code would be harder to use to communicate your criteria, but it is possible. You could even use standard XML parsers to do the heavy parsing lifting (including checking for well-formedness), and simply tack-on any domain checks you require via procedural code.
If you're willing to use some schema but just not W3C XML Schema (XSD), you might consider DTDs, RELAX NG, or Schematron.
Upvotes: 1
Reputation: 161
There is no such thing. Without a schema, the most you can do is check for well-formedness of the document. All other semantics are inside the schema, or must be custom coded.
Upvotes: 0