![](/img/trans.png)
[英]Validating XML which has '& 'against XSD throws "The entity name must immediately follow the '&' in the entity reference"
[英]Validating xml against an xsd which has an include
我试图针对内部引用另一个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元素类型接受“ true”,但不接受“ True”。我如何使其接受? )提供了一些附加信息以及基于枚举值的可能的替代解决方案(如果您无法将True更改为true )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.