Path: blob/master/src/java.xml/share/classes/javax/xml/validation/package-info.java
40948 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/**26* <p>27* Provides an API for validation of XML documents. <em>Validation</em> is the28* process of verifying that an XML document is an instance of a specified XML29* <em>schema</em>. An XML schema defines the content model (also called a30* <em>grammar</em> or <em>vocabulary</em>) that its instance documents will31* represent.32*33* <p>34* There are a number of popular technologies available for creating an XML schema.35* Some of the most popular ones include:36*37* <ul>38* <li><strong>Document Type Definition (DTD)</strong>39* - XML's built-in schema language.40* </li>41* <li><strong><a href="http://www.w3.org/XML/Schema">W3C XML Schema (WXS)</a></strong> -42* an object-oriented XML schema language. WXS also provides a type system43* for constraining the character data of an XML document. WXS is maintained44* by the <a href="http://www.w3.org">World Wide Web Consortium (W3C)</a>45* and is a W3C Recommendation (that is, a ratified W3C standard specification).46* </li>47* <li><strong><a href="http://www.relaxng.org">RELAX NG (RNG)</a></strong> -48* a pattern-based, user-friendly XML schema language. RNG schemas may49* also use types to constrain XML character data. RNG is maintained by50* the <a href="http://www.oasis-open.org">Organization for the Advancement51* of Structured Information Standards (OASIS)</a> and is both an OASIS52* and an <a href="http://www.iso.org">ISO (International Organization53* for Standardization)</a> standard.54* </li>55* <li><strong><a href="http://standards.iso.org/ittf/PubliclyAvailableStandards/c055982_ISO_IEC_19757-3_2016.zip">Schematron</a></strong> -56* a rules-based XML schema language. Whereas DTD, WXS, and RNG are designed57* to express the structure of a content model, Schematron is designed to58* enforce individual rules that are difficult or impossible to express59* with other schema languages. Schematron is intended to supplement a60* schema written in structural schema language such as the aforementioned.61* Schematron is <a href="http://standards.iso.org/ittf/PubliclyAvailableStandards/index.html">an ISO standard</a>.62* </li>63* </ul>64* <p>65* While JAXP supports validation as a feature of an XML parser, represented by66* either a {@link javax.xml.parsers.SAXParser} or {@link javax.xml.parsers.DocumentBuilder}67* instance, the {@code Validation} API is preferred.68*69* <p>70* The JAXP validation API decouples the validation of an instance document from71* the parsing of an XML document. This is advantageous for several reasons,72* some of which are:73*74* <ul>75* <li><strong>Support for additional schema langauges.</strong>76* The JAXP parser implementations support only a subset of the available77* XML schema languages. The Validation API provides a standard mechanism78* through which applications may take of advantage of specialization79* validation libraries which support additional schema languages.80* </li>81* <li><strong>Easy runtime coupling of an XML instance and schema.</strong>82* Specifying the location of a schema to use for validation with JAXP83* parsers can be confusing. The Validation API makes this process simple84* (see <a href="#example-1">example</a> below).85* </li>86* </ul>87* <p>88* <a id="example-1"><strong>Usage example</strong>.</a> The following example89* demonstrates validating an XML document with the Validation API90* (for readability, some exception handling is not shown):91*92* <pre>93*94* // parse an XML document into a DOM tree95* DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();96* Document document = parser.parse(new File("instance.xml"));97*98* // create a SchemaFactory capable of understanding WXS schemas99* SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);100*101* // load a WXS schema, represented by a Schema instance102* Source schemaFile = new StreamSource(new File("mySchema.xsd"));103* Schema schema = factory.newSchema(schemaFile);104*105* // create a Validator instance, which can be used to validate an instance document106* Validator validator = schema.newValidator();107*108* // validate the DOM tree109* try {110* validator.validate(new DOMSource(document));111* } catch (SAXException e) {112* // instance document is invalid!113* }114* </pre>115* <p>116* The JAXP parsing API has been integrated with the Validation API. Applications117* may create a {@link javax.xml.validation.Schema} with the validation API118* and associate it with a {@link javax.xml.parsers.DocumentBuilderFactory} or119* a {@link javax.xml.parsers.SAXParserFactory} instance by using the120* {@link javax.xml.parsers.DocumentBuilderFactory#setSchema(Schema)} and121* {@link javax.xml.parsers.SAXParserFactory#setSchema(Schema)} methods.122* <strong>You should not</strong> both set a schema and call <code>setValidating(true)</code>123* on a parser factory. The former technique will cause parsers to use the new124* validation API; the latter will cause parsers to use their own internal validation125* facilities. <strong>Turning on both of these options simultaneously will cause126* either redundant behavior or error conditions.</strong>127*128*129* @since 1.5130*/131132package javax.xml.validation;133134135