2

我从 @XmlElement 的 JAXB 文档中得到了这个

如果 required() 为真,则 Javabean 属性将映射到具有 minOccurs="1" 的 XML 模式元素声明。maxOccurs 对于单值属性为“1”,对于多值属性为“无界”。

然而当我说

@XmlElement(name = "Name", required = true, nillable = false)

我在 .xsd 中得到以下信息

<xs:element name="Name" type="xs:string"/>

我想知道如何使 minOccurs 等于 1。似乎required = true原因minOccurs消失了

编辑 我意识到默认值为1。无论如何minOccurs在.xsd中显示为1

Edit2 我正在使用 JAXBContext.generateSchema 生成模式(仅供参考)

4

3 回答 3

2

The JAXB spec defines the behaviour:

If required() is true, then Javabean property is mapped to an XML schema element declaration with minOccurs="1". maxOccurs is "1" for a single valued property and "unbounded" for a multivalued property.

The indivual implementations (Metro, EclipseLink MOXy, Apache JaxMe) are free to generate the XML schema as they see fit according to this rule. Metro and MOXy (I'm the tech lead) choose to use the absence of the minOccurs attribute to indicate minOccurs="1".

于 2011-02-03T20:55:01.030 回答
1

我不是 JAXB 专家,但我相信“minOccurs”默认设置为 1,并带有 xs:element 标签。换句话说,如果标记中缺少“minOccurs”属性,则其默认值为 1。

编辑:对不起,直到为时已晚才看到您的编辑!

于 2011-02-03T20:16:58.923 回答
0

xs:element 的默认值为 minOccurs=1。所以它可以省略属性并且仍然具有相同的含义,这可能是 jaxb 正在做的事情。

编辑:看到你的编辑太晚了。我从未见过这样做的方法,但这并不意味着它不可能。

于 2011-02-03T20:17:29.510 回答