I am trying to design an XML schema for books where a unique ID needs to be specified for each book entry. However it just doesn\'t seem to work. Below is the XSD i am using
You've got the in the wrong place - it needs to be inside the definition of the ancestor element within which the Book elements should be unique, not in the Book element definition itself. The following would force Book ids to be unique within each shelf, but would allow the same ID on different shelves:
If instead you want the IDs to be globally unique across all shelves then put the unique constraint at the BookShelf level and adjust the selector appropriately:
For future reference, note that if your schema had a targetNamespace then those selectors would not work as-is, because unprefixed names in selector XPaths always mean "no namespace". You would need to add xmlns:tns=" to your xs:schema element and then use a selector of tns:Shelf/tns:Book.