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:sequence>
...
<xs:element name="id" type="II" minOccurs="0" maxOccurs="unbounded"/>
...
</xs:sequence>
<xs:attribute name="ID" type="xs:ID"/>
...
</xs:complexType>


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.
 bindings.jxb
<jaxb:bindings
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
jaxb:extensionBindingPrefixes="xjc"
version="2.1">
<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>
<jaxb:bindings node="//xs:complexType[@name='POCD_MT000040.RegionOfInterest']/xs:attribute[@name='ID']">
<jaxb:property name="id2"/>
</jaxb:bindings>
<jaxb:bindings node="//xs:complexType[@name='POCD_MT000040.Section']/xs:attribute[@name='ID']">
<jaxb:property name="id2"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>


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

-i

A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

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.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.