xml cvc-complex-type.2.4.a:发现以元素“ProcessDesc”开头的无效内容。预期的 ProcessName 之一

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25174301/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:04:35  来源:igfitidea点击:

cvc-complex-type.2.4.a: invalid content was found starting with element 'ProcessDesc'. One of ProcessName expected

xmljaxbschemaxsd

提问by user656213

I am validating my jaxb object through Validator class. Below is the code I am using to validate jaxb object. But While validating it I am getting this error.

我正在通过 Validator 类验证我的 jaxb 对象。下面是我用来验证 jaxb 对象的代码。但是在验证它时,我收到了这个错误。

jc = JAXBContext.newInstance(obj.getClass());
source = new JAXBSource(jc, obj);
Schema schema = schemaInjector.getSchema();
Validator validator = schema.newValidator();
validator.validate(source);

ERROR(SAXParseException): cvc-complex-type.2.4.a: invalid content was found starting with element 'ProcessDesc'. One of ProcessName expected

错误(SAXParseException):cvc-complex-type.2.4.a:发现以元素“ProcessDesc”开头的无效内容。预期的 ProcessName 之一

I don't understand what I have done wrong in my xsd which is causing this error. The element defined in my xsd file is below for which I am getting an error.

我不明白我在 xsd 中做错了什么导致了这个错误。我的 xsd 文件中定义的元素在下面,我收到了错误消息。

<xs:schema xmlns:cc="http://www.ms.com/cm.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ms.com/cm.xsd" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="Process">
<xs:sequence>
<xs:element name="ProcessId" type="xs:int" />
<xs:element name="ProcessName" type="xs:string" />
<xs:element name="ProcessDesc" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>

Please help me to solve this. Thank You.

请帮我解决这个问题。谢谢你。

回答by laune

The XML Sehema code

XML Sehema 代码

<xs:complexType name="Process">
  <xs:sequence>
    <xs:element name="ProcessId" type="xs:int" />
    <xs:element name="ProcessName" type="xs:string" />
    <xs:element name="ProcessDesc" type="xs:string" minOccurs="0" />
  </xs:sequence>
</xs:complexType>

describes some XML which ought to look like

描述了一些应该看起来像的 XML

<proc> <!-- of type Process -->
  <ProcessId>123</ProcessId>
  <ProcessName>procA</ProcessName>
  <ProcessDesc>A funny process</ProcessDesc> <!-- this could be omitted -->
<proc>

But your XML data looks like

但是您的 XML 数据看起来像

<proc> <!-- of type Process -->
  <ProcessId>123</ProcessId>
  <ProcessDesc>A funny process</ProcessDesc>
  <!-- ... don't know what follows -->

If you don't care about the order of Id, Name, Desc you'll have to change the XML schema. Otherwise you'll have to fix the XML (which is easier).

如果您不关心 Id、Name、Desc 的顺序,则必须更改 XML 模式。否则,您将不得不修复 XML(这更容易)。

If you think that "any order of elements" is a good idea, use:

如果您认为“元素的任意顺序”是个好主意,请使用:

<xs:complexType name="Process">
  <xs:all>
    <xs:element name="ProcessId" type="xs:int" />
    <xs:element name="ProcessName" type="xs:string" />
    <xs:element name="ProcessDesc" type="xs:string" minOccurs="0" />
  </xs:all>
</xs:complexType>

回答by Mohit Singh

These kind of erors are due to one of the following reason

这些错误是由于以下原因之一



Element name is mistyped.

Element not described in schema is trying to be used.

Elements are in incorrect order.

Namespace definitions declared either in the root tag or a parent element don't match with the prefix (or no prefix) used in the element.

Java object has a null field required in xsd

元素名称输入错误。

正在尝试使用架构中未描述的元素。

元素的顺序不正确。

在根标记或父元素中声明的命名空间定义与元素中使用的前缀(或无前缀)不匹配。

Java 对象在 xsd 中需要一个空字段



回答by Xstian

If you use a sequence you must keep the order of each element

如果使用序列,则必须保持每个元素的顺序

Definition and Usage The sequence element specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times.

定义和用法 sequence 元素指定子元素必须出现在一个序列中。每个子元素可以出现 0 次到任意次数。

see here

这里

回答by tituspenaligom

This is a simple XSD validation error ... w3c

这是一个简单的 XSD 验证错误... w3c

回答by Zon

Another option is constraint violationswhen you set a field as requiredand it's NULLin a record. Check your XSD's minOccurselement property.

另一种选择是当您根据需要设置字段并且它在记录中时违反约束。检查您的元素属性。NULLXSDminOccurs