开发者

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

开发者 https://www.devze.com 2023-01-19 21:22 出处:网络
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

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


I know this is not really the answer to your exact question per se, but if the attribute group denotes a common type, consider modelling it as such? I don't know if RNC can do this, but XML Schema can:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.org/simple"
    xmlns:tns="http://www.example.org/simple"
    elementFormDefault="qualified">
    <xsd:complexType name="AmountAttribute">
        <xsd:attribute name="quantity" type="xsd:decimal" />
        <xsd:attribute name="numerator" type="xsd:nonNegativeInteger" />
        <xsd:attribute name="denominator" type="xsd:positiveInteger" />
    </xsd:complexType>

    <xsd:element name="measure">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element minOccurs="0" name="amount" type="tns:AmountAttribute"/>
                <xsd:element minOccurs="0" name="maxAmount" type="tns:AmountAttribute"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

</xsd:schema>

This maps cleanly to two fields amount and maxAmount of the same type.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号