views:

358

answers:

1

My XML looks likes this :

<company>
  <employee id="1">Larsen</employee>
  <employee id="2">Smith</employee>
  <employee id="3">Sam</employee>
</company>

How to write a xml schema so that employee element is defined such a way that each employee has a unique id attribute (no two employee elements can have same value for id attribute)

+4  A: 
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1"
    elementFormDefault="unqualified" attributeFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
      <xs:element name="employee" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:attribute name="ID" type="xs:string" />
        </xs:complexType>

      </xs:element>
      </xs:sequence>
    </xs:complexType>
    <xs:unique name="EmployeeIDKey">
      <xs:selector xpath="./employee" />
      <xs:field xpath="@ID" />
    </xs:unique>
  </xs:element>
</xs:schema>

Edit: beefed it up a bit for you.

womp