【发布时间】:2015-05-20 04:26:08
【问题描述】:
我在使用 XML 模式的唯一约束时遇到了一点问题。
这是要验证的 XML 的一部分:
<pieces>
<whitePieces>
<pawn name="p" taken="false" POSonBoard="12"></pawn>
<pawn name="p" taken="false" POSonBoard="12"></pawn>
<rook name="r" taken="false" POSonBoard="11"></rook>
<rook name="r" taken="false" POSonBoard="81"></rook>
<knight name="n" taken="false" POSonBoard="21"></knight>
<knight name="n" taken="false" POSonBoard="71"></knight>
<bishop name="b" taken="false" POSonBoard="31"></bishop>
<bishop name="b" taken="false" POSonBoard="61"></bishop>
<queen name="q" taken="false" POSonBoard="41"></queen>
<king name="k" taken="false" POSonBoard="51"></king>
</whitePieces>
<blackPieces
(and you get what goes here I'm sure)
(注意顶部的两个棋子如何具有相同的 POSonBoard 值)
这里是 XSD 的一部分(或者我应该只发布整个内容?它很长)
<xs:element name="blackPieces">
<xs:complexType>
<xs:sequence>
<xs:element name="pawn" minOccurs="8" maxOccurs="8">
<xs:complexType>
<xs:attribute name="name" type="xs:string" fixed="P"></xs:attribute>
<xs:attribute name="taken" type="xs:boolean"></xs:attribute>
<xs:attribute ref="POSonBoard"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="rook" minOccurs="2" maxOccurs="10">
<xs:complexType>
<xs:attribute name="name" type="xs:string" fixed="R"></xs:attribute>
<xs:attribute name="taken" type="xs:boolean"></xs:attribute>
<xs:attribute ref="POSonBoard"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="knight" minOccurs="2" maxOccurs="10">
<xs:complexType>
<xs:attribute name="name" type="xs:string" fixed="N"></xs:attribute>
<xs:attribute name="taken" type="xs:boolean"></xs:attribute>
<xs:attribute ref="POSonBoard"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="bishop" minOccurs="2" maxOccurs="10">
<xs:complexType>
<xs:attribute name="name" type="xs:string" fixed="B"></xs:attribute>
<xs:attribute name="taken" type="xs:boolean"></xs:attribute>
<xs:attribute ref="POSonBoard"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="queen" minOccurs="1" maxOccurs="9">
<xs:complexType>
<xs:attribute name="name" type="xs:string" fixed="Q"></xs:attribute>
<xs:attribute name="taken" type="xs:boolean"></xs:attribute>
<xs:attribute ref="POSonBoard"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="king" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:attribute name="name" type="xs:string" fixed="K"></xs:attribute>
<xs:attribute name="taken" type="xs:boolean"></xs:attribute>
<xs:attribute ref="POSonBoard"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
<xs:unique name="testUnique">
<xs:selector xpath="POSonBoard"/>
<xs:field xpath="pieces"/>
</xs:unique>
</xs:element>
我什至不能 100% 确定我将唯一约束放置在正确的位置 (假设根元素和你存在但没有包含在上面以节省一些空间,一切都很好[也许除了唯一约束])
如果有人能引导我朝着正确的方向前进,那就太棒了! (另外,如果您发现还有什么做得不好的地方,请告诉)
【问题讨论】:
标签: xml validation xsd unique