控制 JAXB 为 xsd:attributeGroup 生成的类名?
我正在使用 JAXB 将 XML 绑定到我正在编写的应用程序的 Java。我有一个名为 measure 的元素,其中包含两个名为 amount 和 maxAmount 的 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>
JAXB 从此创建了以下内容的更详细版本:
public class Measure {
protected Measure.Amount amount;
protected Measure.MaxAmount maxAmount;
public static class Measure.Amount {}
public static class Measure.MaxAmount {}
}
Measure.Amount 和 Measure.MaxAmount 是相同的除了它们的名字之外,当然,就 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 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道这本身并不是您确切问题的答案,但如果属性组表示常见类型,请考虑对其进行建模?我不知道 RNC 是否可以做到这一点,但 XML 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:
This maps cleanly to two fields amount and maxAmount of the same type.