XPath Unqiue元素不起作用

时间:2015-05-20 13:38:21

标签: xml xpath xsd

这是我的架构:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="chessGame">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="pawns" type="pawns" minOccurs="1" maxOccurs="1"/>
        <xs:element name="knights" type="knights" minOccurs="1" maxOccurs="1"/>
        <xs:element name="kings" type="kings" minOccurs="1" maxOccurs="1"/>
        <xs:element name="queens" type="queens" minOccurs="1" maxOccurs="1"/>
        <xs:element name="rooks" type="rooks" minOccurs="1" maxOccurs="1"/>
        <xs:element name="bishops" type="bishops" minOccurs="1" maxOccurs="1"/>
      </xs:sequence>
      <xs:attribute name="dateTime" type="xs:dateTime" use="required"/>
    </xs:complexType>
    <xs:unique name="uniquePosition">
      <xs:selector xpath="chessgame"/>
      <xs:field xpath="@position"/>
    </xs:unique>
  </xs:element>

  <xs:complexType name="pawns">
    <xs:sequence>
      <xs:element name="whitePawn" type="chessPiece" minOccurs="8" maxOccurs="8"/>
      <xs:element name="blackPawn" type="chessPiece" minOccurs="8" maxOccurs="8"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="knights">
    <xs:sequence>
      <xs:element name="whiteKnight" type="chessPiece" minOccurs="2" maxOccurs="2"/>
      <xs:element name="blackKnight" type="chessPiece" minOccurs="2" maxOccurs="2"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="kings">
    <xs:sequence>
      <xs:element name="whiteKing" type="chessPiece" minOccurs="1" maxOccurs="1"/>
      <xs:element name="blackKing" type="chessPiece" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="queens">
    <xs:sequence>
      <xs:element name="whiteQueen" type="chessPiece" minOccurs="1" maxOccurs="1"/>
      <xs:element name="blackQueen" type="chessPiece" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="bishops">
    <xs:sequence>
      <xs:element name="whiteBishop" type="chessPiece" minOccurs="2" maxOccurs="2"/>
      <xs:element name="blackBishop" type="chessPiece" minOccurs="2" maxOccurs="2"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="rooks">
    <xs:sequence>
      <xs:element name="whiteRook" type="chessPiece" minOccurs="2" maxOccurs="2"/>
      <xs:element name="blackRook" type="chessPiece" minOccurs="2" maxOccurs="2"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="chessPiece">
    <xs:attribute name="position" use="required">
      <xs:simpleType>
        <xs:restriction base="xs:integer">
          <xs:maxInclusive value="88"/>
          <xs:minInclusive value="11"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="isCaptured" type="xs:boolean" use="required"/>
  </xs:complexType>
</xs:schema>

我希望每个chessPiece的位置都是唯一的,但我找不到所应用的唯一元素的在线任何模式示例,它实际上将模式分解为单独的complexTypes和simpleTypes。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

问题来自您的选择器。您希望在<chessgame>元素上设置@position属性,而需要检查唯一性的标记则低两级。

这个选择器工作正常:

<xs:selector xpath="*/*"/>

如果你需要选择器不如上面那么一般,你可以使用这个 - 用详尽的标签列表完成:

<xs:selector xpath="pawns/whitePawn | pawns/blackPawn | knights/whiteKnight | knights/blackKnight"/>