users@jaxb.java.net

Wrong marshalling of xsd:any type

From: Florian Huonder <fhuonder_at_herasaf.org>
Date: Tue, 3 Nov 2009 17:53:47 +0100

Hi all,

 

I have the following schema fragment:

 

<complexType name="StatusDetailType">

   <complexContent>

     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">

       <sequence>

         <any/>

       </sequence>

     </restriction>

   </complexContent>

 </complexType>

 

 

The corresponding java class looks like this:

 

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "StatusDetailType", propOrder = {

    "content"

})

public class StatusDetailType

    implements Serializable

{

 

    private final static long serialVersionUID = 632768732L;

    

    @XmlAnyElement(lax = true)

    protected List<Object> content;

 

    public List<Object> getContent() {

        if (content == null) {

            content = new ArrayList<Object>();

        }

        return this.content;

    }

}

 

A type that can be placed into the any attribute is e.g. the
MissingAttributeDetailType.

Schema of this type looks like:

<complexType name="MissingAttributeDetailType">
   <complexContent>
     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
       <sequence>
         <element
ref="{urn:oasis:names:tc:xacml:2.0:context:schema:os}AttributeValue"
maxOccurs="unbounded" minOccurs="0"/>
       </sequence>
       <attribute name="AttributeId" use="required"
type="{http://www.w3.org/2001/XMLSchema}anyURI" />
       <attribute name="DataType" use="required"
type="{http://www.w3.org/2001/XMLSchema}anyURI" />
       <attribute name="Issuer"
type="{http://www.w3.org/2001/XMLSchema}string" />
     </restriction>
   </complexContent>
 </complexType>

 

And the corresponding java class is:

@XmlRootElement

@XmlAccessorType(XmlAccessType.FIELD)

@XmlType(name = "MissingAttributeDetailType", propOrder = {
"attributeValues" })

public class MissingAttributeDetailType implements Serializable {

 

      private final static long serialVersionUID = 632768732L;

      @XmlElement(name = "AttributeValue")

      protected List<AttributeValueType> attributeValues;

      @XmlAttribute(name = "AttributeId", required = true)

      @XmlSchemaType(name = "anyURI")

      protected String attributeId;

      @XmlAttribute(name = "DataType", required = true)

      @XmlJavaTypeAdapter(URNToDataTypeConverter.class)

      @XmlSchemaType(name = "anyURI")

      protected DataTypeAttribute<?> dataType;

      @XmlAttribute(name = "Issuer")

      protected String issuer;

 

.

.

 

}

 

Now when I marshall the StatusDetailType class this results in:

<StatusDetail>

    <missingAttributeDetailType
DataType="http://www.w3.org/2001/XMLSchema#string"
AttributeId="urn:oasis:names:tc:xacml:1.0:resource:resource-id"/>

</StatusDetail>

 

But this is wrong because it should marshallt to:

<StatusDetail>

    <MissingAttributeDetail
DataType="http://www.w3.org/2001/XMLSchema#string"
AttributeId="urn:oasis:names:tc:xacml:1.0:resource:resource-id"/>

</StatusDetail>

 

 

I use JAXB 2.1.12.

A second question: Why is the @XmlRootElement needed in the
MissingAttributeDetailType class? Without it, there is an exception asking
for this annotation.

 

I hope somebody can help me.

Thanks a lot in advance.

 

Regards

Florian