XSD attributeGroup可选吗?

时间:2009-03-27 06:43:44

标签: xml xsd

我在XSD架构中定义了一个attributeGroup。从我读过和试过的内容来看,我无法指定整个组的使用的可选性(因此指定了所有属性,或者没有指定),例如:

 <xs:attributeGroup name="NameDesc">
    <xs:attribute name="n" type="xs:string" use="required"/>
    <xs:attribute name="d" type="xs:string" use="required"/>
    <xs:attribute name="uinf" type="xs:string" use="required"/>
  </xs:attributeGroup>

  <xs:complexType name="g">
    <xs:attributeGroup ref="tns:NameDesc" use="optional" />
  </xs:complexType>

(注意 use =“required”我在attributeGroup引用中添加了,这是不允许的)。我是对的还是有办法实现这个目标?

我已阅读How to make "use" attribute (required/optional) depend on another value?,但我不确定这适用于我的情况。

2 个答案:

答案 0 :(得分:4)

据我所知(如果我错了,请有人纠正我),不可能让属性依赖于此。

你能考虑让attributeGroup成为一个真正的元素吗?这样,整个元素可以作为子元素存在或不存在。

如果您将架构映射到对象,这也是一个更好的设计。

答案 1 :(得分:-1)

如果不使用attributeGroup,还有另一种方法可以做到这一点。你可以使用类似的东西:

 <xs:complexType name="g">
 <xs:sequence>
  <xs:attribute name="type"/>
  <xs:sequence minOccurs="0" maxOccurs="1">
     <xs:attribute name="n" type="xs:string" use="required"/>
     <xs:attribute name="d" type="xs:string" use="required"/>
     <xs:attribute name="uinf" type="xs:string" use="required"/>
  </xs:sequence>
 </xs:sequence>
</xs:complexType>

其中 minOccurs =“0”maxOccurs =“1”使3元素属性组成为可选项,如果使用了所有元素,则需要全部。

参考:W3 Wiki