Link to home
Start Free TrialLog in
Avatar of N M
N MFlag for Luxembourg

asked on

XSD: Check in sets of attributes, that values aren't duplicates

Is it possible to check duplicate attribute values within same element using an XSD ?

For example:

<MyProduct  category="school" code="123">Pencil</MyProduct>
<MyProduct category="home" code="343">Vase</MyProduct>
<MyProduct category="home" code="752">Fork</MyProduct>
<MyProduct category="school" code="123">Eraser</MyProduct>

Open in new window


In the above example, a pencil and an eraser belong to same category and have same code. How can I control that no product ("MyProduct") can have same category *and* code?
(if possible)


Note: in above example, categories and codes have a specific list of values (i.e. I know I have 12 categories and 728 codes).
Thank you
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In order to detect uniqueness, you need a scope.
So I have assumed a parent MyRoot
In that context you set the xs:unique
nice thing about xs:unique is that you can set two xs:field, so you can mandate the uniqueness of a set rather than a single value
Avatar of N M

ASKER

Checking your solution, much appreciated.
My XML begins like this:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<report xmlns="http://www.w3.org/2001/XMLSchema-instance">
   <control>....</control>
   <data>...</data>
   <audit>...</audit>
</report>

Open in new window


May I assume that the "report" is the root element ?
report is the root. But if all MyProduct are grouped in a single container node, it is good for performance not to require uniqueness over the entire document, but just on the container node... it is wise to choose the context well
Avatar of N M

ASKER

My container node is the <data> node. However, the XML might have multiple (identical as per the structure) data nodes. I.e.

<?xml version="1.0" encoding="ISO-8859-1"?> 
<report xmlns="http://www.w3.org/2001/XMLSchema-instance">
   <control>....</control>
       <data>...</data>
       <data>...</data>
       <data>...</data>
        ...
   <audit>...</audit>
</report>

Open in new window


and inside <data> I have <MyProduct> elements among other things. Should I use the <data> ?
If MyProduct needs to be unique within a single data node, use data for the xs:unique if you need uniqueness on a bigger scope, choose a bigger scope. It is the requirement that drives the scope. That should be your call, not mine
Avatar of N M

ASKER

Very accurate and fully working. Thank you very much for the extra advices.