[英]Validating xml against an xsd which has an include
I'm trying to validate an XML against an XSD which internally refers to another XSD (using include statement). 我试图针对内部引用另一个XSD的XSD验证XML(使用include语句)。
as, 如,
<xs:include schemaLocation="Schema2.xsd"/>
Now while validating my XML against the xsd(schema1.xsd) like: 现在,针对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;
}
I get an error, "cvc-datatype-valid.1.2.1: 'True' is not a valid value for 'boolean'." 我收到一个错误,“ cvc-datatype-valid.1.2.1:'True'不是'boolean'的有效值。”
This is for an element which is defined in the schema2.xsd which schema1.xsd is referring. 这是针对schema1.xsd所引用的schema2.xsd中定义的元素的。
Please tell me if I am doing something wrong. 请告诉我我做错了什么。
You are experiencing a case problem. 您遇到案例问题。 'true' is a valid value for xsd:boolean, but 'True' (which is apparently what is in the XML document) is not. “ true”是xsd:boolean的有效值,但“ True”(显然是XML文档中的值)不是。
The responses here ( xsd:boolean element type accept "true" but not "True". How can I make it accept it? ) provide some additional information and a possible alternate solution based on enumerated values (if you cannot change the True to true). 此处的响应( xsd:boolean元素类型接受“ true”,但不接受“ True”。我如何使其接受? )提供了一些附加信息以及基于枚举值的可能的替代解决方案(如果您无法将True更改为true )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.