-1

I'm writing a xml schema. I want some node to be unique based upon some inner element. I wrote the below schema but it is not working. I am able to add same book/employee more than once. I want my book name/employee is should be unique and through exception if someone enters duplicate element(node) can someone help me in this? Thanks in advance.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3schools.com" targetNamespace="http://www.w3schools.com" elementFormDefault="unqualified">

<xs:complexType name="employee">
    <xs:sequence>
        <xs:attribute name="name" type="xs:string" use="required"/>
        <xs:attribute name="id" type="xs:string" use="required"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="employees">
    <xs:sequence>
        <xs:element name="employees" type="employee" minOccurs="0" maxOccurs="unbounded">
            <xs:unique name="uniqueId">
                <xs:selector xpath="employee"/>
                <xs:field xpath="@id"/>
            </xs:unique>
        </xs:element>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="book">
    <xs:sequence>
        <xs:attribute name="author" type="xs:string" use="required"/>
        <xs:attribute name="title" type="xs:string" use="required"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="bookList">
    <xs:sequence>
        <xs:element name="bookList" type="book" minOccurs="0" maxOccurs="unbounded">
            <xs:unique name="uniqueBook">
                <xs:selector xpath="book"/>
                <xs:field xpath="@title"/>
            </xs:unique>
        </xs:element>
    </xs:sequence>
</xs:complexType>

<xs:element name="Library">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="bookList" type="bookList"/>
            <xs:element name="employees" type="employees"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

</xs:schema>
patrick
  • 357
  • 6
  • 20

1 Answers1

0

Your schema says that the id attribute of employee elements must be unique, but both in your schema and in your instance (posted as a comment), the employee element does not have an id attribute; instead it has a title attribute.

Furthermore, your employee and book elements are declared in the schema to be in a namespace (though this namespace is missing from your instance), and you have ignored this namespace in defining your selector/field XPath expressions in your uniqueness constraints.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Sorry, I'm not going to try and debug code with known typos. Do us the courtesy of getting your question right. – Michael Kay Jun 06 '17 at 08:13
  • I know issue is relate to selector/field xpath. can you help me in this? I'm new to this thing. – patrick Jun 06 '17 at 08:37
  • I followed https://stackoverflow.com/questions/7717515/unique-constraint-in-xml-schema for my case book should be unique based upon book' title. thats why selector xpath is book and field xpath is title – patrick Jun 06 '17 at 08:52