controlling the class names generated by JAXB for xsd:attributeGroup?

Posted by Stephen Winnall on Stack Overflow See other posts from Stack Overflow or by Stephen Winnall
Published on 2010-10-14T19:53:59Z Indexed on 2011/01/08 22:54 UTC
Read the original article Hit count: 235

Filed under:
|
|
|

I am using JAXB to bind XML to Java for an application that I am writing. I have an element called measure which contains two amount elements called amount and maxAmount, with which I want to model a lower and an upper limiting value. amount and maxAmount are otherwise identical and I would like them to be implemented with the same class when unmarshalled into Java.

The following is an extract from the XML schema which I feed to JAXB:

<xsd:attributeGroup name="AmountAttributes">
  <xsd:attribute name="quantity" type="xsd:decimal"/>
  <xsd:attribute name="numerator" type="xsd:nonNegativeInteger"/>
  <xsd:attribute name="denominator" type="xsd:positiveInteger"/>
</xsd:attributeGroup>

<xsd:element name="measure">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element minOccurs="0" name="amount">
        <xsd:complexType>
          <xsd:attributeGroup ref="mpr:AmountAttributes"/>
        </xsd:complexType>
      </xsd:element>
      <xsd:element minOccurs="0" name="maxAmount">
        <xsd:complexType>
          <xsd:attributeGroup ref="mpr:AmountAttributes"/>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

JAXB creates from this a more elaborate version of the following:

public class Measure {

  protected Measure.Amount amount;
  protected Measure.MaxAmount maxAmount;

  public static class Measure.Amount {}
  public static class Measure.MaxAmount {}
}

Measure.Amount and Measure.MaxAmount are identical except for their names, but - of course - as far as Java is concerned they have little to do with each other.

Is there a way of making JAXB use the same class for both amount and maxAmount?

Just to come completely clean ;-) I should mention that I generate the XML schema from RNC using Trang. If the answer to the question is "change the XML schema", I have the supplementary question "how do I change the RNC to produce that XML schema?". My RNC looks like this:

AmountAttributes =
  QuantityAttribute?
  & attribute numerator { xsd:nonNegativeInteger }?
  & attribute denominator { xsd:positiveInteger }?
QuantityAttribute = attribute quantity { xsd:decimal }

Measure =
  element measure {
    element amount { AmountAttributes }?,
    element maxAmount { AmountAttributes }?
  }+

I use RNC because I find it simpler to understand, but if the solution to my problem means just using XML Schema, so be it.

Steve

© Stack Overflow or respective owner

Related posts about xml-schema

Related posts about jaxb