自动将一类用于多个元素

问题描述

对不起,我使用xjc的经验很少。我有一个xsd,其中包含很多(杂乱无章的)元素,例如:

<xs:element name="foo">
  <xs:complexType>
    <xs:attribute name="V" type="xs:anySimpleType" use="optional"/>
  </xs:complexType>
</xs:element>

<xs:element name="bar">
  <xs:complexType>
    <xs:attribute name="V" type="xs:anySimpleType" use="optional"/>
  </xs:complexType>
</xs:element>

它们只是名称不同。使用xjc,我得到了类似这样的类:

@XmlAccessorType(XmlAccesstype.FIELD)
@XmlType(name = "")
public static class Foo {

    @XmlAttribute(name = "V")
    @XmlSchemaType(name = "anySimpleType")
    protected String v;

    public String getV() {
        return v;
    }

    public void setV(String value) {
        this.v = value;
    }
}

如果我手动创建此类,并命名为ElemWithStrAttr,是否可以指示xjc对与“具有单个String属性'V'的元素”模式匹配的所有xsd元素使用此类?我的意思是不必在某些绑定或情节文件中显式命名应映射到此类的所有xsd元素?

感谢帮助!

解决方法

我认为,如果您更改xsd,以便为属性创建复杂类型,并对每个元素使用该类型,您仍然可以使用xjc生成的文件。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="foo" type="VType"/>
                <xs:element name="bar" type="VType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="VType">
        <xs:attribute name="V" type="xs:anySimpleType" use="optional"/>
    </xs:complexType>
</xs:schema>