Xsd 是否可以定义包含属性组的元素组?

Xsd 是否可以定义包含属性组的元素组?,xsd,Xsd,我正在定义一个包含多个复杂元素的xsd,这些元素在不同的元素下重复类似的结构。 样本: 定义以下内容的正确方法是什么?我发现在使用此代码段时无法解析架构 <xs:complexType name="keyType"> <xs:sequence maxOccurs="unbounded"> <xs:element name="add"> <xs:complexType> <xs:attributeGr

我正在定义一个包含多个复杂元素的xsd,这些元素在不同的元素下重复类似的结构。 样本:


定义以下内容的正确方法是什么?我发现在使用此代码段时无法解析架构

<xs:complexType name="keyType">
  <xs:sequence maxOccurs="unbounded">
    <xs:element name="add">
      <xs:complexType>
        <xs:attributeGroup ref="keyAttributeGroup"/>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType> 

<xs:attributeGroup name="keyAttributeGroup">
  <xs:attribute name="key" type="xs:string" use="required"/>
  <xs:attribute name="value" type="xs:string" use="required"/>
</xs:attributeGroup>

如果您事先知道keyType类型的元素可能具有的所有名称(
添加键、删除键
),则一种可能的解决方案是使用未命名的选项

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="keys">
        <xs:complexType>
                <xs:choice maxOccurs="unbounded">
                    <xs:element name="add-key" type="keyType" />
                    <xs:element name="del-key" type="keyType" />
                </xs:choice>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="keyType">
        <xs:sequence maxOccurs="unbounded">
            <xs:element name="add">
                <xs:complexType>
                    <xs:attributeGroup ref="keyAttributeGroup" />
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:attributeGroup name="keyAttributeGroup">
        <xs:attribute name="key" type="xs:string" use="required" />
        <xs:attribute name="value" type="xs:string" use="required" />
    </xs:attributeGroup>
</xs:schema>


如果元素可以有任何随机名称(
addkey,delkey,随便什么),我知道最接近的解决方案是使用
xs:any

这绝对是一个有趣的方式来看待它!不过,我正在寻找一种方法来定义一个元素组,其中每个元素下都有一个属性组。我提供的例子是我面临的一般问题的一个独特案例。但是,我必须再说一次,你的回答很好!
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="keys">
        <xs:complexType>
                <xs:choice maxOccurs="unbounded">
                    <xs:element name="add-key" type="keyType" />
                    <xs:element name="del-key" type="keyType" />
                </xs:choice>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="keyType">
        <xs:sequence maxOccurs="unbounded">
            <xs:element name="add">
                <xs:complexType>
                    <xs:attributeGroup ref="keyAttributeGroup" />
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:attributeGroup name="keyAttributeGroup">
        <xs:attribute name="key" type="xs:string" use="required" />
        <xs:attribute name="value" type="xs:string" use="required" />
    </xs:attributeGroup>
</xs:schema>