2

This problem is taking me too long to fix

I could use some guidance

I'm trying to define a game board and i want to be sure each house can only exist once, meaning it has a set of coordinates unique.

I don't understand why my code accepts repeated values of Coordinates

<xs:element name="Board">
    <xs:complexType>
        <xs:sequence minOccurs="3" maxOccurs="unbounded">
            <xs:element name="house" type="Tile">
                <xs:unique name="tileKey">
                    <xs:selector xpath="./Point"/>
                    <xs:field xpath="x"/>
                    <xs:field xpath="y"/>
                </xs:unique>                
            </xs:element>           
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="Tile">
    <xs:sequence>
        <xs:element name="Point" type="Coords"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="Coords">
    <xs:sequence>
        <xs:element name="x" type="xs:int"/>
        <xs:element name="y" type="xs:int"/>
    </xs:sequence>
</xs:complexType>

If i have 3 houses with x and y 0 the xml is valid. What am I doing wrong?

output is something like

<Board>
<house>
    <Point>
        <x>0</x>
        <y>0</y>
    </Point>
</house>
</Board>

Thank you for your help

0

1 Answer 1

3

Your unique validation is only applying to the points within a house (of which there can only be one by your schema definition, so the validation would never trigger). If you want to validate across all points in all house tags, you need to move your unique check out one level:

<xs:element name="Board">
  <xs:complexType>
    <xs:sequence minOccurs="3" maxOccurs="unbounded">
      <xs:element name="house" type="Tile">
      </xs:element>           
    </xs:sequence>
  </xs:complexType>
  <xs:unique name="tileKey">
    <xs:selector xpath="./house/Point"/>
    <xs:field xpath="x"/>
    <xs:field xpath="y"/>
  </xs:unique>                
</xs:element>
1
  • another question... the unique tag must be in the end? because im using xmlspy and gives me problems if i paste it before complexType... it makes me wonder if it ignores the unique Commented Apr 2, 2012 at 16:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.