xml - Making Element Values Unique -


i have small xml project i'm doing defining schema chess game state made in xml. includes creating chess piece elements , creating child elements these pieces reference position in pgn (positional game notation).

the document continues defining king, queen etc , position(s). how ensure value within position tag unique i.e., pawn cannot have 2 positions @ 17 having 2 pieces on same square of board.

<position>17</position> <position>17</position> 

i tried using unique constraint freaked out @ fact have multiple position elements within piece element. how make value of these pieces unique , no 2 pieces can share same position value?

edit: placement of unique constraint works position elements within same piece tag, there way position throughout whole document. example:

<board-side name="white">     <pieces>         <piece name="pawn" symbol="p">             <position>12</position>  <board-side name="black">     <pieces>         <piece name="pawn" symbol="p">             <position>17</position> 

both of above xml fragments within own board-side tags. there way make if change position of black board side 12, throw unique constraint error, or unique work multiple elements within same tag?

edit 2: whole schema

<?xml version="1.0" encoding="utf-8"?> <xs:schema id="chess_schema" xmlns:xs="http://www.w3.org/2001/xmlschema">      <xs:simpletype name="ident">         <xs:restriction base="xs:string">             <xs:enumeration value="p" />             <xs:enumeration value="p" />             <xs:enumeration value="q" />             <xs:enumeration value="q" />             <xs:enumeration value="r" />             <xs:enumeration value="r" />             <xs:enumeration value="n" />             <xs:enumeration value="n" />             <xs:enumeration value="b" />             <xs:enumeration value="b" />             <xs:enumeration value="k" />             <xs:enumeration value="k" />         </xs:restriction>     </xs:simpletype>      <xs:element name="game-state">         <xs:complextype>             <xs:sequence>                  <xs:element name="board-state">                     <xs:complextype>                         <xs:sequence>                              <xs:element name="board-side" minoccurs="2" maxoccurs="2">                                 <xs:complextype>                                     <xs:sequence>                                          <xs:element name="pieces">                                             <xs:complextype>                                                 <xs:sequence>                                                      <xs:element name="piece" maxoccurs="6">                                                         <xs:complextype>                                                             <xs:sequence>                                                                  <xs:element name="position" minoccurs="0" maxoccurs="8" >                                                                     <xs:simpletype>                                                                         <xs:restriction base="xs:int">                                                                             <xs:mininclusive value="11" />                                                                             <xs:maxinclusive value="88" />                                                                         </xs:restriction>                                                                     </xs:simpletype>                                                                 </xs:element>                                                              </xs:sequence>                                                             <xs:attribute name="symbol" type="ident" />                                                             <xs:attribute name="name" type="xs:string" />                                                         </xs:complextype>                                                     </xs:element>                                                  </xs:sequence>                                             </xs:complextype>                                         </xs:element>                                          <xs:element name="taken-pieces" maxoccurs="1" minoccurs="0" type="xs:string" />                                      </xs:sequence>                                     <xs:attribute name="name" type="xs:string" />                                 </xs:complextype>                             </xs:element>                          </xs:sequence>                     </xs:complextype>                 </xs:element>              </xs:sequence>         </xs:complextype>          <xs:unique name="uniquepos">             <xs:selector xpath="board-side/pieces/piece/position" />             <xs:field xpath="." />         </xs:unique>      </xs:element> <!--end game-state root--> </xs:schema> 

an uniqueness constraint guarantees position elements unique within specified scope.

you should define constraint in element represents scope. if scope piece element, example, can associate position elements via xpath selector relative context.

if have no target namespace can use without prefixes:

<xs:element name="piece">     <xs:complextype>         <xs:sequence>             <xs:element ref="position" minoccurs="0" maxoccurs="8"/>         </xs:sequence>     </xs:complextype>     <xs:unique name="uniquepos">         <xs:selector xpath="position" />         <xs:field xpath="." />     </xs:unique> </xs:element> 

if schema has target namespace, however, must declare prefix since xpath assumes unprefixed name belongs no-namespace. need like:

<xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema"             elementformdefault="qualified"            xmlns="ns1" xmlns:chess="ns1" targetnamespace="ns1">  ... 

(you don't need duplicate default namespace, unless refer unqualified types somewhere else, need prefixed one).

then can prefix xpath selectors:

<xs:unique name="uniquepos">     <xs:selector xpath="chess:position" />     <xs:field xpath="." /> </xs:unique> 

update considering edition.

if want increase scope uniqueness constraint valid, have place declaration in scope. example, if want positions unique within entire game-state element, can declare there:

<xs:element name="game-state">     ...     <xs:unique name="uniquepos">         <xs:selector xpath="/board-state/board-side/pieces/piece/position" />         <xs:field xpath="." />     </xs:unique> </xs:element> 

this cause validation error if there 2 position elements same value in context below game-state. can put in other places, long xpath expression valid in context. example, in board-state be:

<xs:element name="board-state">     ...     <xs:unique name="uniquepos">         <xs:selector xpath="board-side/pieces/piece/position" />         <xs:field xpath="." />     </xs:unique> </xs:element> 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -