我正在为我的java项目构建一个带有xerces-2_11_0-xml-schema-1.1-beta库的xml验证器,以支持XSD 1.1特定的功能,如断言 .
考虑这个架构
<?xml version="1.1" encoding="UTF-8"?>
<schema targetNamespace="http://www.example.org/Example" elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.org/Example">
<complexType name="NodeType">
<sequence>
<element name="Node" type="tns:NodeType"
maxOccurs="unbounded" minOccurs="0">
</element>
</sequence>
<attribute name="partnumber">
<simpleType>
<restriction base="string">
<pattern value="[A-Z0-9_\-]+"></pattern>
</restriction>
</simpleType>
</attribute>
<assert test="starts-with(@partnumber,../@partnumber)"/>
</complexType>
<element name="Node" type="tns:NodeType"></element>
</schema>
我开始使用“javax.xml.validation.Validator”类
...
StreamSource xmlSource = new StreamSource(new File("example.xml"));
SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");
Schema s = sf.newSchema(new File("example.xsd"));
Validator v = s.newValidator();
v.setErrorHandler(new MyErrorHandler())
v.validate(xmlSource)
...
private static class MyErrorHandler extends DefaultHandler {
...
public void error(SAXParseException e) throws SAXException {
System.out.println("Error: ");
System.out.println(" Public ID: "+e.getPublicId());
System.out.println(" System ID: "+e.getSystemId());
System.out.println(" Line number: "+e.getLineNumber());
System.out.println(" Column number: "+e.getColumnNumber());
System.out.println(" Message: "+e.getMessage());;
}
...
}
此解决方案有效:example.xsd中的断言测试得到正确处理,xml文件的验证运行顺利(正确评估断言测试)
然后我用SAXParser替换Validator(原因:更好地控制sax解析阶段)
...
SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");
Schema s = sf.newSchema(new File("example.xsd"));
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setSchema(s);
spf.setNamespaceAware(true);
spf.setValidating(true);
spf.setFeature("http://apache.org/xml/features/validation/schema", true);
SAXParser parser = spf.newSAXParser();
XMLReader r = parser.getXMLReader();
r.setErrorHandler(new MyErrorHandler())
r.parse("example.xml");
...
在第二种情况下,解析阶段在xsd“assert”子句上遇到错误,并显示以下消息:
s4s-elt-invalid-content.1: The content of 'NodeType' is invalid. Element 'assert' is invalid, misplaced, or occurs too often.
如果我删除example.xsd中的assert子句(从而使其符合1.0),验证运行正确,所以我认为SAXParser仍在使用1.0规范 .
我是否遗漏了配置SAXParserFactory或SAXParser本身以便坚持1.1架构规则的内容?
1 回答
我认为您的项目中缺少XPATH 2.0依赖性 . 请检查一下!
来自:http://xerces.apache.org/xerces2-j/faq-xs.html