我正在尝试针对内部引用另一个 XSD(使用 include 语句)的 XSD 验证 XML。
作为,
<xs:include schemaLocation="Schema2.xsd"/>
现在在针对 xsd(schema1.xsd) 验证我的 XML 时,例如:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(
inputXml)));
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
Source src = new DOMSource(document);
Result xmlFile = new StreamResult(new File("xmlFileName.xml"));
aTransformer.transform(src, xmlFile);
} catch (Exception e) {
// TODO: handle exception
}
File xmlFile = new File("xmlFileName.xml");
SchemaFactory factory1 = SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema");
File schemaLocation = new File("F:\\Project\\Automation\\XSDs\\schema1.xsd");
Schema schema = factory1.newSchema(schemaLocation);
Validator validator = schema.newValidator();
Source source = new StreamSource(xmlFile);
try {
validator.validate(source);
System.out.println(xmlFile.getName() + " is valid.");
isXmlValid = true;
} catch (SAXException ex) {
System.out.println(xmlFile.getName() + " is not valid because ");
System.out.println(ex.getMessage());
isXmlValid = false;
}
我收到一个错误,“cvc-datatype-valid.1.2.1:'True' 不是 'boolean' 的有效值。”
这是针对在 schema1.xsd 所指的 schema2.xsd 中定义的元素。
如果我做错了什么,请告诉我。