控制JAXB为xsd:attributeGroup生成的类名?

控制JAXB为xsd:attributeGroup生成的类名?,jaxb,xsd,xjc,relaxng,Jaxb,Xsd,Xjc,Relaxng,我正在使用JAXB为我正在编写的应用程序将XML绑定到Java。我有一个名为measure的元素,它包含两个名为amount和maxantum的amount元素,我想用它们来建模一个下限值和一个上限值。amount和maxAmount在其他方面是相同的,我希望在将它们解组到Java中时使用相同的类实现它们 以下是我提供给JAXB的XML模式的摘录: <xsd:attributeGroup name="AmountAttributes"> <xsd:attribute nam

我正在使用JAXB为我正在编写的应用程序将XML绑定到Java。我有一个名为measure的元素,它包含两个名为amount和maxantum的amount元素,我想用它们来建模一个下限值和一个上限值。amount和maxAmount在其他方面是相同的,我希望在将它们解组到Java中时使用相同的类实现它们

以下是我提供给JAXB的XML模式的摘录:

<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>
Amount和Measure.maxantum除了名称之外是相同的,但是——当然——就Java而言,它们之间几乎没有什么关系

有没有办法让JAXB对amount和maxAmount使用相同的类

只是为了彻底澄清;-)我应该提到,我使用Trang从RNC生成XML模式。如果问题的答案是“更改XML模式”,那么我还有一个补充问题“如何更改RNC以生成该XML模式?”。我的RNC如下所示:

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

Measure =
  element measure {
    element amount { AmountAttributes }?,
    element maxAmount { AmountAttributes }?
  }+
我使用RNC是因为我发现它更容易理解,但是如果我的问题的解决方案意味着只使用XML模式,那就这样吧


史提夫

< P>我知道这本身并不是你的确切问题的答案,但是如果属性组表示一个普通的类型,那么就考虑把它建模成这样的吗?我不知道RNC是否可以做到这一点,但XML模式可以:

<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>


这是两个字段的数量和最大的相同类型的映射。

我知道这本身并不是真正的问题的答案,但是如果属性组表示一个普通的类型,那么就把它建模为一个普通的类型。我不知道RNC是否可以做到这一点,但XML模式可以:

<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>


这将清晰地映射到同一类型的两个字段amount和maxAmount。

谢谢。看起来我要么遇到了RNC的限制,要么遇到了我对RNC知识的限制。我想我必须考虑迁移到XSD。我确信你可以通过编写一个模式编译器插件来处理这个RNC XSD阻抗不匹配,但我担心这将是一场艰难的斗争。祝你的项目好运。谢谢。看起来我要么遇到了RNC的限制,要么遇到了我对RNC知识的限制。我想我必须考虑迁移到XSD。我确信你可以通过编写一个模式编译器插件来处理这个RNC XSD阻抗不匹配,但我担心这将是一场艰难的斗争。祝你的项目好运。