1
我试图通过不同元素类型的共享属性唯一性约束添加。所有这些元素共享一组通用属性,使用attributeGroup
定义。独特属性
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:attributeGroup name="commonAttributes">
<xs:attribute name="id" type="xs:string" use="required" />
<xs:attribute name="displayName" type="xs:string" />
</xs:attributeGroup>
...
<xs:element name="mainType" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="firstType" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attributeGroup ref="commonAttributes"></xs:attributeGroup>
</xs:complexType>
</xs:element>
<xs:element name="secondType" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attributeGroup ref="commonAttributes"></xs:attributeGroup>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
...
</xs:schema>
基本上,两者firstType
和secondType
元素定义id
属性,它需要有唯一值进行的跨每个mainType
实例。从我读过的内容来看,unique
约束不能在xs:attributeGroup
内设置。在firstType
和secondType
元素上设置此限制显然仅适用于该类型的其他元素,这意味着firstType
元素的实例可以具有与secondType
元素相同的id
值。
在mainType
元素中定义的所有类型中,是否有使id
属性唯一的方法?将单个元素名称设置为属性将意味着重大的代码更改和规范的隐式更改(我非常想不触发)。
是的,这确实起作用。从中可以看出,XPath有助于抽象不同的元素类型。无论节点名称如何,“*”的意思是“所有孩子”。 –