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

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.


Hope you found this post useful...

...so please read on! I love writing articles that provide beneficial information, tips and examples to my readers. All information on my blog is provided free of charge and I encourage you to share it as you wish. There is a small favour I ask in return however - engage in comments below, provide feedback, and if you see mistakes let me know.

If you want to show additional support and help me pay for web hosting and domain name registration, donations, no matter how small, are always welcome!

Use of any information contained in this blog post/article is subject to this disclaimer.
comments powered by Disqus
Other posts you may like...