Java 错误:发现以元素“X”开头的无效内容。'{X}' 之一是预期的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18462941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: Invalid content was found starting with element 'X'. One of '{X}' is expected
提问by Eduardo Leggiero
I'm trying to validate a simple XML using a simple XSD, but always get this error:
我正在尝试使用简单的 XSD 验证简单的 XML,但总是收到此错误:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'linux'. One of '{linux}' is expected.
Why? The tag 'linux' is found and is one of {linux}!
为什么?标签“linux”被找到并且是 {linux} 之一!
java code:
代码:
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());
}
}
data/test.xml:
数据/test.xml:
<?xml version="1.0" encoding="utf-8"?>
<so xmlns="http://test/">
<linux>
<debian>true</debian>
<fedora>true</fedora>
</linux>
</so>
data/test.xsd
数据/测试.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>
采纳答案by Michael Kay
Because the schema does not specify elementFormDefault="qualified"
, the local element declaration of element "linux" is declaring an element in no namespace, but the instance has a linux element in namespace "http://test/"
. The error message is confusing because it fails to make clear that the problem is with the namespace.
由于架构没有指定elementFormDefault="qualified"
,元素“linux”的本地元素声明是在没有命名空间中声明一个元素,但实例在命名空间中有一个 linux 元素"http://test/"
。错误消息令人困惑,因为它没有说明问题出在命名空间上。
回答by Erik Pragt
Dudytz, your <xs:any />
is not correct. The document cannot be validated, because the validator needs to be instructed how to validate the document. If you don't want that, you can specifiy for the <xs:any>
a processContents attribute. If you set this to "lax" or "skip", it will work.
Dudytz,你的说法<xs:any />
不正确。无法验证文档,因为需要指示验证者如何验证文档。如果您不希望那样,您可以指定<xs:any>
processContents 属性。如果您将其设置为“宽松”或“跳过”,它将起作用。
In short: replace <xs:any>
with <xs:any processContents="lax" />
简而言之:替换<xs:any>
为<xs:any processContents="lax" />
Update: We have changed your XSD to a working version:
更新:我们已将您的 XSD 更改为工作版本:
<xs:schema xmlns="http://test/" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test/">
<xs:element name="so">
<xs:complexType>
<xs:sequence>
<xs:element ref="linux"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="linux">
<xs:complexType>
<xs:sequence>
<xs:any processContents="lax" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>