3

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Records">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Contract">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="Records">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element type="xs:string" name="General"/>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

When generating POJO from above XSD, occurs error "Records is already defined in package"

And I want to know, is my XSD is valid? Can we create complexType inside of another with the same name as its upper element?

Bagdat
  • 307
  • 2
  • 13

1 Answers1

3

This is legal in XSD. However, XJC has known issues with name clashes, which you can override in the JAXB bindings. In this answer I explained a few days ago how that can be done. The solution is the same, though the cause of your error is different.

Note that, as you mentioned in the comments, it is irrelevant what names are used, as long as you tell JAXB what XSD element to map with what Java member. The (de)serializer will make sure that this is properly round-trippable.

Something like:

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               version="2.1">
    <jaxb:bindings schemaLocation="yourschemalocation.xsd">
        <jaxb:bindings node="//xs:element[@name='Contract']
            /xs:complexType/xs:sequence/xs:element[@name='Records']
            /xs:complexType">
            <jaxb:class name="NestedRecords"/>
        </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>

You can add the bindings to your commandline using the -b option: xjc -d out -b binding.xml yourschemalocation.xsd, where binding.xml is the file above.

Another alternative, if you have control over the XSD schema, is to use XSD Annotations to control the generated classnames, as explained by this answer.

Community
  • 1
  • 1
Abel
  • 52,738
  • 19
  • 137
  • 227
  • thanks for fast reply. But, i cannot understand, what if I cannot make changes in XSD? Where does above snippet of code should be inserted? – Bagdat Sep 30 '15 at 11:16
  • @Bagdat, I have updated the answer. Simply specify it on the commandline. I also added a link to the commandline reference. – Abel Sep 30 '15 at 11:47
  • It means that, POJO will be generated with name NestedRecord? After generating POJO I have to fill with the data and create XML, and generated XML must be valid based on first given XSD? I suppose that it will not be valid, yes? – Bagdat Sep 30 '15 at 11:59
  • @Bagdat, the whole idea of mapping an XSD to code is to allow parsing and validation of XML input that conforms to the XSD. It is irrelevant how the names in the POJO are called, as long as the (de)serializer knows what elements or attributes to map it with. A similar approach is necessary, for instance, if the XML names use characters not allowed in Java method names. So, yes it will be valid, and yes you can fill it with data, and yes the created XML will have the correct structure. No worries, it looks different, but maps to the same ;). – Abel Sep 30 '15 at 12:18
  • accepted, your answer satisfies the initial question, thanks – Bagdat Oct 08 '15 at 14:18