如何在xsd-schema中声明xml-element

时间:2013-07-23 18:40:43

标签: xml jaxb xsd

我有xml元素:

<attribute name="attributeName1" 
           type="typeName1">value1</attribute>
...
<attribute name="attributeName2" 
           type="typeName2">
    <row order="1">
        <attribute name="attributeName3" 
                   type="typeName3">value3</attribute>
        ...
    </row>
    ...
 </attribute>

如何在xsd-schema中声明这些元素?

1 个答案:

答案 0 :(得分:0)

您可以定义attribute元素,使其具有混合内容的类型。如下所示:

<xsd:element name="attribute">
    <xsd:complexType mixed="true">
        <xsd:sequence>
            <xsd:element name="row" minOccurs="0" maxOccurs="unbounded">
                <xsd:complexType>
                    ...
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

这将为您提供类似于以下内容的JAXB类,其中您最终会得到一个将存储文本和Row对象的属性。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "content"
})
@XmlRootElement(name = "attribute")
public class Attribute {

    @XmlElementRef(name = "row", namespace = "http://www.example.org/schema", type = JAXBElement.class, required = false)
    @XmlMixed
    protected List<Serializable> content;

}

如果你做了类似下面的事情,你可能会从长远来看更快乐。

<attribute name="attributeName1" type="typeName1">
    <value>value1</value>
</attribute>