Skip to main content

@XmlElementRef(required=false) Doesn't Generate minOccurs="0"

1 reply [Last post]
Joined: 2010-06-09

I already logged an issue ( but haven't had a response yet so thought I'd post here to to see if anyone has found a work around.

The @XmlElementRef.required() attribute was added in bug 389. I cannot get
@XmlElementRef(required=false) to generate minOccurs="0" for and element with
schemagen. Here is a sample JUnit test that demonstrates the issue:


import javax.xml.bind.*;
import javax.xml.bind.annotation.*;

import org.junit.*;

public class SchemagenXmlElementRefRequiredBug {

private static final class ReferencedClass {

private ReferencedClass() {}

public int mInt;

private static final class ClassWithReference {

private ClassWithReference() {}

public ReferencedClass mNotRequiredRef;

public ReferencedClass mRequiredRef;

public void SchemagenToConsole() throws Exception {

private static void writeXSDToConsole(final JAXBContext context)
throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();

SchemaGenerator.generateSchema(context, out);


Where SchemaGenerator.generateSchema(context, out) uses
JAXBContext.generateSchema(SchemaOutputResolver) to write the XSD to an
OutputStream. I omitted that code for brevity. The above generates the following

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

------- Additional comments from jenglert Mon Jun 7 22:20:11 +0000 2010 -------


Java 1.6.0u18
JAXB 2.2.1 added with "-Xbootclasspath/p:lib/jaxb-util.jar"

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2010-06-09

Just following up with the solution, also noted here:

Basically, I forgot to add jaxb-impl.jar to the classpath. So, while the API was correct by adding jaxb-api.jar to the bootclasspath the underlying implementation found in jaxb-impl.jar was missing. Adding jaxb-impl.jar to the classpath was missing from the docs but I suppose could be considered common sense.