我正在尝试使用简单的XSD验证简单的XML,但始终会收到此错误:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'linux'. One of '{linux}' is expected.
Run Code Online (Sandbox Code Playgroud)
为什么?标签'linux'被找到并且是{linux}之一!
java代码:
public static void main(String[] args) {
try {
InputStream xml = new FileInputStream("data/test.xml");
InputStream xsd = new FileInputStream("data/test.xsd");
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new StreamSource(xsd));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(xml));
log.info("OK!");
} catch (Exception e) {
log.error(":(");
log.error(e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
数据/的test.xml:
<?xml version="1.0" encoding="utf-8"?>
<so xmlns="http://test/">
<linux>
<debian>true</debian>
<fedora>true</fedora>
</linux>
</so>
Run Code Online (Sandbox Code Playgroud)
数据/ test.xsd
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://test/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="so">
<xs:complexType>
<xs:sequence>
<xs:element name="linux">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:any processContents="lax" maxOccurs="unbounded"/>
</xs:sequence></xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
Mic*_*Kay 13
因为模式没有指定elementFormDefault="qualified",元素"linux"的本地元素声明声明了没有命名空间中的元素,但实例在命名空间中有一个linux元素"http://test/".错误消息令人困惑,因为它无法清楚问题是否与命名空间有关.
| 归档时间: |
|
| 查看次数: |
11871 次 |
| 最近记录: |