Home | History | Annotate | Download | only in validation
      1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
      2 <!-- $Id: package.html 570104 2007-08-27 13:28:24Z mrglavas $ -->
      3 <html>
      4 	<body bgcolor="white">
      5 		<p>
      6 		    This package provides an API for validation of XML documents.  <em>Validation</em> is the process of verifying
      7 		    that an XML document is an instance of a specified XML <em>schema</em>.  An XML schema defines the
      8 		    content model (also called a <em>grammar</em> or <em>vocabulary</em>) that its instance documents
      9 		    will represent.
     10         </p>
     11         <p>
     12             There are a number of popular technologies available for creating an XML schema. Some of the most
     13             popular include:
     14             <ul>
     15                 <li><strong>Document Type Definition (DTD)</strong> - XML's built-in schema language.</li>
     16                 <li><strong><a href="http://www.w3.org/XML/Schema">W3C XML Schema (WXS)</a></strong> - an object-oriented XML schema
     17                     language. WXS also provides a type system for constraining the character data of an XML document.
     18                     WXS is maintained by the <a href="http://www.w3.org">World Wide Web Consortium (W3C)</a> and is a W3C
     19                     Recommendation (that is, a ratified W3C standard specification).</li>
     20                 <li><strong><a href="http://www.relaxng.org">RELAX NG (RNG)</a></strong> - a pattern-based,
     21                     user-friendly XML schema language. RNG schemas may also use types to constrain XML character data.
     22                     RNG is maintained by the <a href="http://www.oasis-open.org">Organization for the Advancement of
     23                     Structured Information Standards (OASIS)</a> and is both an OASIS and an
     24                     <a href="http://www.iso.org">ISO (International Organization for Standardization)</a> standard.</li>
     25                 <li><strong><a href="http://www.schematron.com/">Schematron</a></strong> - a rules-based XML schema
     26                 language. Whereas DTD, WXS, and RNG are designed to express the structure of a content model,
     27                 Schematron is designed to enforce individual rules that are difficult or impossible to express
     28                 with other schema languages. Schematron is intended to supplement a schema written in
     29                 structural schema language such as the aforementioned. Schematron is in the process
     30                 of becoming an ISO standard.</li>
     31             </ul>
     32         </p>
     33         <p>
     34 		    Previous versions of JAXP supported validation as a feature of an XML parser, represented by
     35 		    either a {@link javax.xml.parsers.SAXParser} or {@link javax.xml.parsers.DocumentBuilder} instance.
     36         </p>
     37         <p>
     38 		    The JAXP validation API decouples the validation of an instance document from the parsing of an
     39 		    XML document. This is advantageous for several reasons, some of which are:
     40 		    <ul>
     41 		        <li><strong>Support for additional schema languages.</strong> As of JDK 1.5, the two most
     42 		        popular JAXP parser implementations, Crimson and Xerces, only support a subset of the available
     43 		        XML schema languages. The Validation API provides a standard mechanism through which applications
     44 		        may take of advantage of specialization validation libraries which support additional schema
     45 		        languages.</li>
     46 		        <li><strong>Easy runtime coupling of an XML instance and schema.</strong> Specifying the location
     47 		        of a schema to use for validation with JAXP parsers can be confusing. The Validation API makes this
     48 		        process simple (see <a href="#example-1">example</a> below).</li>
     49           </ul>
     50 		</p>
     51 		<p>
     52             <a name="example-1"><strong>Usage example</strong>.</a> The following example demonstrates validating
     53             an XML document with the Validation API (for readability, some exception handling is not shown):
     54             <pre>
     55             
     56     // parse an XML document into a DOM tree
     57     DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
     58     parserFactory.setNamespaceAware(true);
     59     DocumentBuilder parser = parserFactory.newDocumentBuilder();
     60     Document document = parser.parse(new File("instance.xml"));
     61 
     62     // create a SchemaFactory capable of understanding WXS schemas
     63     SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
     64 
     65     // load a WXS schema, represented by a Schema instance
     66     Source schemaFile = new StreamSource(new File("mySchema.xsd"));
     67     Schema schema = factory.newSchema(schemaFile);
     68 
     69     // create a Validator instance, which can be used to validate an instance document
     70     Validator validator = schema.newValidator();
     71 
     72     // validate the DOM tree
     73     try {
     74         validator.validate(new DOMSource(document));
     75     } catch (SAXException e) {
     76         // instance document is invalid!
     77     }
     78 </pre>
     79 		</p>
     80 		<p>
     81 		    The JAXP parsing API has been integrated with the Validation API. Applications may create a {@link javax.xml.validation.Schema} with the validation API
     82 		    and associate it with a {@link javax.xml.parsers.DocumentBuilderFactory} or a {@link javax.xml.parsers.SAXParserFactory} instance
     83 		    by using the {@link javax.xml.parsers.DocumentBuilderFactory#setSchema(Schema)} and {@link javax.xml.parsers.SAXParserFactory#setSchema(Schema)}
     84 		    methods. <strong>You should not</strong> both set a schema and call <code>setValidating(true)</code> on a parser factory. The former technique
     85 		    will cause parsers to use the new validation API; the latter will cause parsers to use their own internal validation
     86 		    facilities. <strong>Turning on both of these options simultaneously will cause either redundant behavior or error conditions.</strong>
     87         </p>
     88         <p>
     89 
     90 	</body>
     91 </html>
     92