XML1.1中的变更不是向下兼容的,XML1.0中的一些well-formedness规则在,XML1.1中可能就不适用。所以XML1.1的规范是全新的而不是从XML1.0规范上升级。为了能够使用XML1.1和XML1.1的名字空间,你必须在XML序言声明中把version属性的值设为 “1.1”。例如:
<?xml version="1.1" encoding="UTF-8" standalone="yes"?>
XInclude允许一个XML文档包含另一个XML文档,例如:
<myMainXMLDoc xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include href="fragment.xml"/>
...
</myMainXMLDoc>
相要使用XML inclusions特性,你必须在适当的factory里设置XInclude属性为true,就像下面代码所示:
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
dbfactory.setXIncludeAware(true);
根据预解析的schema校验JAXP的输入源
javax.xml.validation包提供了解析schema和根据预解析的schema校验XML文档的功能。DOMSource和 SAXSource是可以根据预解析的schema来被校验。如果需要可以缓存预解析的schema来达到优化的目的。必须注意到的是,根据预解析的 schema校验JAXP的输入源中,StreamSource并不是被支持的源,还有schema可以是W3C XML Schema 或者是一个OASIS RELAX-NG。例如:
//parse an XML in non-validating mode and create a DOMSource
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
dbfactory.setNamespaceAware(true);
dbfactory.setXIncludeAware(true);
DocumentBuilder parser = dbfactory.newDocumentBuilder();
Document doc = parser.parse(new File("data.xml"));
DOMSource xmlsource = new DOMSource(doc);
//create a SchemaFactory for loading W3C XML Schemas
SchemaFactory wxsfactory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
//set the errorhandler for handling errors in schema itself
wxsfactory.setErrorHandler(schemaErrorHandler);
//load a W3C XML Schema
Schema schema = wxsfactory.newSchema(new File("myschema.xsd"));
// create a validator from the loaded schema
Validator validator = schema.newValidator();
//set the errorhandler for handling validation errors
validator.setErrorHandler(validationErrorHandler);
//validate the XML instance
validator.validate(xmlsource);
