9

I have a XSD like the following:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" version="1.0">
    <xs:element name="foo">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="bar">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
                                <xs:attribute name="type" use="required">
                                    <xs:simpleType>
                                        <xs:restriction base="xs:string">
                                            <xs:enumeration value="a"/>
                                            <xs:enumeration value="b"/>
                                        </xs:restriction>
                                    </xs:simpleType>
                                </xs:attribute>
                                <xs:attribute name="subtype" use="required">
                                    <xs:simpleType>
                                        <xs:restriction base="xs:string">
                                            <xs:enumeration value="aa"/>
                                            <xs:enumeration value="bb"/>
                                            <xs:enumeration value="cc"/>
                                            <xs:enumeration value="dd"/>
                                            <xs:enumeration value="ee"/>
                                            <xs:enumeration value="ff"/>
                                        </xs:restriction>
                                    </xs:simpleType>
                                </xs:attribute>
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
                <xs:element name="something">
                    <xs:complexType>
                        <xs:attribute name="name" type="xs:string" use="required"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

but JAXB does not generate enums:

    @XmlAttribute(name = "type", required = true)
    protected String type;
    @XmlAttribute(name = "subtype", required = true)
    protected String subtype;

I'm using the maven-jaxb2-plugin to generate the JAXB classes.

How can I generate enums in this case? Why doesn't it work out-of-the-box?

Please note that I cannot change the XML structure. I'd prefer a custom binding solution (using a xjb file), but I might be able to change the XSD as well, as long as it doesn't change the XML structure.

Xstian
  • 8,184
  • 10
  • 42
  • 72
Puce
  • 37,247
  • 13
  • 80
  • 152
  • @LucaBassoRicci Firstly it's an attribute not an element. Secondly a top level element has some other semantics (is a valid root element). – Puce Dec 16 '15 at 12:20

2 Answers2

8

Thanks to https://stackoverflow.com/a/666722/506855 I managed to define the following bindings file, which did the trick:

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              version="2.1">

    <jxb:bindings schemaLocation="testError.xsd">
        <jxb:bindings node="//xs:attribute[@name='type']/xs:simpleType">
            <jxb:typesafeEnumClass name="Type" />
        </jxb:bindings>
        <jxb:bindings node="//xs:attribute[@name='subtype']/xs:simpleType">
            <jxb:typesafeEnumClass name="Subtype" />
        </jxb:bindings>
    </jxb:bindings>

</jxb:bindings>

There is also a related (and old) JAXB issue: https://java.net/jira/browse/JAXB-318

Community
  • 1
  • 1
Puce
  • 37,247
  • 13
  • 80
  • 152
3

Your enum is an inner simpleType. Change your xsd in

    <xs:element name="foo">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="bar">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
                                <xs:attribute name="type" use="required">
                                    <xs:simpleType>
                                        <xs:restriction base="xs:string">
                                            <xs:enumeration value="a" />
                                            <xs:enumeration value="b" />
                                        </xs:restriction>
                                    </xs:simpleType>
                                </xs:attribute>
                                <xs:attribute name="subtype" use="required" type="CodeEnum"/>
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
                <xs:element name="something">
                    <xs:complexType>
                        <xs:attribute name="name" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:simpleType name="CodeEnum">
        <xs:restriction base="xs:string">
            <xs:enumeration value="aa" />
            <xs:enumeration value="bb" />
            <xs:enumeration value="cc" />
            <xs:enumeration value="dd" />
            <xs:enumeration value="ee" />
            <xs:enumeration value="ff" />
        </xs:restriction>
    </xs:simpleType>

Note: For this solution the xml will be the same.

Xstian
  • 8,184
  • 10
  • 42
  • 72
  • Thanks, but I didn't try since I found a solution using a bindings file so I don't have to change the XSD. – Puce Dec 16 '15 at 12:18