views:

94

answers:

4

How can i define an boolean attribute that can be set "true" only in one element. Following snippet must be invalid.

<products>
  <product featured="yes">Prod 1</product>
  <product featured="yes">Prod 2</product>
</products>
+3  A: 

You can't do that with XML Schemas.

You can define attributes on an element, but not limit them to one instance of the element.

Oded
+2  A: 

You could add an attribute in products element indicating which product is featured.

paolot
Thanks for your advice.
Jozef
@AOI Karasu: I think that check it's definitely not possible
paolot
+3  A: 

You can't do this with XMLSchema. If you want to specify these constraints in an XML environment try Schematron (http://www.schematron.com/).

peter.murray.rust
+1  A: 

My reply is this way, 'cause I cannot add comments yet.

"You could add an attribute in products element indicating which product is featured."

This solution lead to another problem: checking if the attribute points to existing element.

<products featured_id="3">
  <product id="1">Prod 1</product>
  <product id="2">Prod 2</product>
</products>
AOI Karasu