Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

NOTE: This article is 3 years or older so its information may no longer be relevant. Read on at your own discretion! Comments for this article have automatically been locked, refer to the FAQ for more details.
If you're working with the CDA XML schema or one of it's derivatives like the Australian CDA Schema Extension and trying to use xjc to compile it into a set of usable Java classes you may be hitting this error...
 xjc error
trying to create the same field twice: id

The issue comes from a name collision between an attribute and a child element within certain complex types in this schema. Specifically it is the id element and the ID attribute. The schema has something like this...
<xs:complexType name="POCD_MT000040.ObservationMedia">
<xs:element name="id" type="II" minOccurs="0" maxOccurs="unbounded"/>
<xs:attribute name="ID" type="xs:ID"/>

I would argue that the ID attribute is of little value...
The purpose of the xs:ID datatype is to define unique identifiers that are global to a document and emulate the ID attribute type available in the XML DTDs.

This attribute can be used for referencing parts of a document within the document itself but has no real application outside of the document scope. However, the id element has application outside of the scope of the document as well as within it. So since the ID attribute is of little importance the solution is to rename it to something that will not cause a name collision.

Rather than modifying the XSD itself, I like to use a bindings file to customise the output of xjc. A binding file like the one below would allow xjc to compile the schema successfully.
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
<jaxb:bindings schemaLocation="POCD_MT000040-AU-V1_0.xsd">
<jaxb:bindings node="//xs:complexType[@name='POCD_MT000040.ObservationMedia']/xs:attribute[@name='ID']">
<jaxb:property name="id2"/>
<jaxb:bindings node="//xs:complexType[@name='POCD_MT000040.RegionOfInterest']/xs:attribute[@name='ID']">
<jaxb:property name="id2"/>
<jaxb:bindings node="//xs:complexType[@name='POCD_MT000040.Section']/xs:attribute[@name='ID']">
<jaxb:property name="id2"/>

The bindings look for these ID attributes on specific objects and rename them to 'id2'. It's as simple as that.


Skip down to comments...
A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. If you spot something out of place, please do let me know.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
comments powered by Disqus
Other posts you may like...