My question is, if i have to validate if just one has an attribute true, and one of all have to be true, using the xml:
<addresses>
<address>
<primary>true</primary>
<street>One str.</street>
<number>111</number>
</address>
<address>
<primary>false</primary>
<street>Two str.</street>
<number>222</number>
</address>
</addresses>
How to do??
ps: Sorry about my english.
The simplest way to do this (or so it seems to me! others may disagree) is to use a slightly different XML structure. You want one primary address and zero or more non-primary addresses. Any XML validation method (DTDs, XSD, …) makes it easy to say that, if you use different names for the things that have different requirements. If the XML structure becomes
then it’s easy to write an XSD schema that constrains
addressesto contain exactly one primary address, and that explicitly captures the fact that primary addresses and other addresses have the same structure:If you cannot change your XML structure, or do not want to, you can switch to XSD 1.1 and use an assertion to enforce the constraint, or you can use Schematron to do so.
Or (this is a slightly dirty trick), you can say (1) that the only legal value for the
primaryattribute istrue, (2) that the attribute is optional on theaddresselement, and (3) that each occurrence of theprimaryattribute must have a unique value.But I really recommend changing the XML structure.