users@jaxb.java.net

namespaces marshalled incorrectly using _at_XmlAnyElement

From: Jason Harrop <jharrop_at_gmail.com>
Date: Thu, 27 Mar 2008 11:16:25 +1100

Hi

I'm using JAXB to unmarshal/marshal XML which looks like this:

                <a:clrScheme name="Office"
xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
                        <a:dk1>
                                <a:sysClr val="windowText" lastClr="000000" />
                        </a:dk1>

I wanted to model the contents using xsd:any, as per this schema fragment:

<xsd:schema
        targetNamespace="http://schemas.openxmlformats.org/drawingml/2006/main"
        elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns="http://schemas.openxmlformats.org/drawingml/2006/main"
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:version="2.1">


                        <xsd:element name="clrScheme" minOccurs="1" maxOccurs="1">
                                        <xsd:complexType>
                                                <xsd:sequence>
                                                        <xsd:any processContents="skip" maxOccurs="unbounded"
                                                                minOccurs="0"/>
                                                </xsd:sequence>
                                        <xsd:attribute name="name" type="xsd:string" use="required">
                                                <xsd:annotation>
                                                        <xsd:documentation>Name</xsd:documentation>
                                                </xsd:annotation>
                                        </xsd:attribute>
                                        </xsd:complexType>
                                </xsd:element>

I used xjc from jaxb-ri-20070917 with -target 2.0 to generate this Java:

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "any"
    })
    public static class CustClrLst {

        @XmlAnyElement
        protected List<Element> any;

        public List<Element> getAny() {
            if (any == null) {
                any = new ArrayList<Element>();
            }
            return this.any;
        }

    }


When I marshall it using the JAXB in Java 6, the resulting XML looks like this:

                <clrScheme name="Office"
xmlns="http://schemas.openxmlformats.org/drawingml/2006/main">
                        <dk1>
                                <sysClr xmlns:ns2="http://schemas.openxmlformats.org/drawingml/2006/main"
xmlns="" lastClr="000000" val="windowText" />
                        </dk1>

Comapred to the original, the sysClr element has xmlns="", so it is in
the wrong namespace.

This seems to be the same problem as
http://forums.java.net/jive/thread.jspa?threadID=37907

That poster observed JAXB is changing the default namespace to "" for
elements that contain attributes, which is consistent with the
behaviour I am seeing. I've replied to that thread noting my similar
experience.

cheers

Jason