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

发布于 2024-09-27 08:42:34 字数 2120 浏览 9 评论 0原文

我正在使用 JAXB 将 XML 绑定到我正在编写的应用程序的 Java。我有一个名为 measure 的元素,其中包含两个名为 amountmaxAmountamount 元素,我想用它们进行建模下限值和上限值。 amountmaxAmount 在其他方面是相同的,我希望它们在解组到 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>

JAXB 从此创建了以下内容的更详细版本:

public class Measure {

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

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

Measure.AmountMeasure.MaxAmount 是相同的除了它们的名字之外,当然,就 Java 而言,它们彼此之间没有什么关系。

有没有办法让 JAXB 对 amountmaxAmount 使用相同的类?

只是为了完全干净;-) 我应该提到我使用 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 Schema,那就这样吧。

史蒂夫

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掌心的温暖 2024-10-04 08:42:34

我知道这本身并不是您确切问题的答案,但如果属性组表示常见类型,请考虑对其进行建模?我不知道 RNC 是否可以做到这一点,但 XML Schema 可以:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文