java - 发现以元素“描述”开头的无效内容。'{"http://test.com/test/v1.0":description}' 之一是预期的
问题描述
爪哇代码:
public static boolean validateXMLSchema(){
try {
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File("students.xsd"));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File("students.xml")));
} catch (IOException e){
System.out.println("Exception: "+e.getMessage());
return false;
}catch(SAXException e1){
System.out.println("SAX Exception: "+e1.getMessage());
return false;
}
学生.xsd 文件
<?xml version = "1.0"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:midi="http://test.com/test/v1.0">
<xs:import namespace="http://test.com/test/v1.0" schemaLocation="midi.xsd"/>
<xs:element name = 'class'>
<xs:complexType>
<xs:sequence>
<xs:element name = 'student' type = 'StudentType' minOccurs = '1'
maxOccurs = '1' />
<xs:element name='rule' type='midi:rule' minOccurs = '1'
maxOccurs = '1' />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name = "StudentType">
<xs:sequence>
<xs:element name = "firstname" type = "xs:string"/>
<xs:element name = "marks" type = "xs:positiveInteger"/>
</xs:sequence>
<xs:attribute name = 'rollno' type = 'xs:positiveInteger'/>
</xs:complexType>
</xs:schema>
midi.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://test.com/test/v1.0"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://test.com/test/v1.0">
<xs:complexType name="rule">
<xs:sequence>
<xs:element name="description" type="xs:string" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<!-- <xs:element name="rule" type="rule"/>-->
</xs:schema>
和我的 xml 文件
<?xml version = "1.0"?>
<class>
<student rollno = "593">
<firstname>Jasvir</firstname>
<marks>90</marks>
</student>
<rule >
<description>test</description>
</rule>
</class>
当我执行 java 代码时,任何人都可以帮助我,然后我得到以下错误 SAX 异常:cvc-complex-type.2.4.a:发现以元素“描述”开头的无效内容。需要 '{" http://test.com/test/v1.0 ":description}' 之一。无效反对
解决方案
推荐阅读
- javascript - Google Chrome 网络状态问题
- angular - 单元测试用例:期望从构造函数中调用组件函数
- excel - 带有变量和 R1C1 的 vba Sumif
- c# - await Task.CompletedTask 是否意味着异步方法将同步运行?
- php - 检查产品是否已经在购物车中
- r - 通过 WDI 加载 Wold Bank 数据的有效方法
- laravel - 如何使用 Laravel Query Builder 同时进行树连接?
- scala - 有没有办法让 GNU xgettext (或类似的东西)与 scala 文件一起工作?
- mysql - 通过比较 2 个表中的 2 个列来获取 MySQL 中的重复记录
- reactjs - 浏览器缓存存储问题