3

可以在模式级别强制使用不同名称的两个属性的唯一值吗?

<xs:complexType name="exampleType">
  <xs:attribute name="first" type="xs:integer" use="required"/>
  <xs:attribute name="second" type="xs:integer"/>
</xs:complexType>

如果first为 1,则second需要为其他值。

编辑:我正在使用 xsd 1.0。

4

1 回答 1

4

正如我在评论中所说,这仅在first并且second是元素(不是属性)时才有效。|xpath 运算符就像节点的联合。

<xs:element name="exampleElement" type="exampleType">
    <xs:unique name="firstAndSecondDifferent">
        <xs:selector xpath="first | second" />
        <xs:field xpath="." />
    </xs:unique>
</xs:element>

<xs:complexType name="exampleType">
    <xs:sequence>
        <xs:element name="first" type="xs:integer" />
        <xs:element name="second" type="xs:integer" minOccurs="0" />
    </xs:sequence>
</xs:complexType>

以下元素是有效的(first并且second具有不同的值):

<exampleElement>
    <first>1</first>
    <second>5</second>
</exampleElement>

以下元素有效(不存在second元素):

<exampleElement>
    <first>1</first>
</exampleElement>

以下元素无效(first并且second具有相同的值):

<exampleElement>
    <first>1</first>
    <second>1</second>
</exampleElement>

这不能用属性来完成,因为selector xpath不允许使用属性,所以"@first | @second"它不是选择器的 xpath 属性的有效值。

于 2014-07-31T14:11:22.780 回答