我正在尝试针对内部引用另一个 XSD 的 XSD 验证 XML(使用 include 语句)。
作为,
<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 中定义的元素。
如果我做错了什么,请告诉我。
最佳答案
您遇到了案例问题。 'true' 是 xsd:boolean 的有效值,但 'True'(显然是 XML 文档中的值)不是。
此处的响应 ( xsd:boolean element type accept "true" but not "True". How can I make it accept it? ) 提供了一些附加信息以及基于枚举值的可能的替代解决方案(如果您无法将 True 更改为 true)。
关于java - 根据包含包含内容的 xsd 验证 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10936419/