2

I have an externally supplied XSD which has many elements all which are identical and represent a price with optional currency attribute.

<xs:complexType name="someType">
  <xs:sequence>
    <xs:element name="rrp" minOccurs="0">
      <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:decimal">
            <xs:attribute name="currency" type="xs:string"/>
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:element>
    <xs:element name="whs" minOccurs="0">
      <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:decimal">
            <xs:attribute name="currency" type="xs:string"/>
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

I'd like this to be transformed into a common class instead of lots of static classes, is this possible with an XJB mapping document?

Brett Ryan
  • 26,937
  • 30
  • 128
  • 163

1 Answers1

2

I don't know a way to do this with the JAXB annotations, but that's not to say that's not possible. Maybe some one will give you a better answer there.

The ideal way to solve this is obviously to change the XSD so that you have a "PriceWithcurrency" xs:complexType definition, and each element is declared to be of that type. But you said you can't change it.

Once thing I would try though, is to create a variant of that XSD that does just that, keep the same namespace definition for it and generate the jaxb classes. It should work with the XML document instances produced using the original XSD's.

Patrice M.
  • 4,209
  • 2
  • 27
  • 36
  • Thanks Patrice, I have indeed been modifying the vendor supplied XSD, though it's not something I wish to rely on as if the XSD changes it will break if not updated once again; this involves documenting what changes I've made for my team to know etc. These XSD's have been annotated as being created with a tool, you'd think the tool would warn against using anonymous types in the first place. I will accept your answer if I do not hear of a transforming way soon, thanks. – Brett Ryan Sep 12 '12 at 15:53