Question: Is it possible to restrict the number of times an element can have a certain value through XSD?

Scenario: I have an element <ITEM> which has three child elements:

<ITEM_BOOLEAN> <NAME_ELEMENT> <ITEM_MEMO_ELEMENT>

<ITEM_BOOLEAN> is restricted to only allow the values of "Y" or "N" is XSD

I would like to restrict that only one occurance of <ITEM> may contain the value "Y" in the child element <ITEM_BOOLEAN> and all other occurances of <ITEM_BOOLEAN> must be "N" for all other <ITEM> elements

What I want to validate: Only one occurance of "Y" in <ITEM_BOOLEAN>

<ROOT> 
<ITEM>
   <ITEM_BOOLEAN>Y</ITEM_BOOLEAN> 
   <NAME_ELEMENT>Foo<NAME_ELEMENT>
   <ITEM_MEMO_ELEMENT>Bar<ITEM_MEMO_ELEMENT>
</ITEM>
<ITEM>
   <ITEM_BOOLEAN>N</ITEM_BOOLEAN> 
   <NAME_ELEMENT>Foo<NAME_ELEMENT>
   <ITEM_MEMO_ELEMENT>Bar<ITEM_MEMO_ELEMENT>
</ITEM>

<ITEM>
   <ITEM_BOOLEAN>N</ITEM_BOOLEAN> 
   <NAME_ELEMENT>Foo<NAME_ELEMENT>
   <ITEM_MEMO_ELEMENT>Bar<ITEM_MEMO_ELEMENT>
</ITEM>
</ROOT>

Should Not Validate: Multiple Occurances of "Y" in <ITEM_BOOLEAN>

<ROOT>
<ITEM>
   <ITEM_BOOLEAN>Y</ITEM_BOOLEAN> 
   <NAME_ELEMENT>Foo<NAME_ELEMENT>
   <ITEM_MEMO_ELEMENT>Bar<ITEM_MEMO_ELEMENT>
</ITEM>
<ITEM>
   <ITEM_BOOLEAN>Y</ITEM_BOOLEAN> 
   <NAME_ELEMENT>Foo<NAME_ELEMENT>
   <ITEM_MEMO_ELEMENT>Bar<ITEM_MEMO_ELEMENT>
</ITEM>

<ITEM>
   <ITEM_BOOLEAN>N</ITEM_BOOLEAN> 
   <NAME_ELEMENT>Foo<NAME_ELEMENT>
   <ITEM_MEMO_ELEMENT>Bar<ITEM_MEMO_ELEMENT>
</ITEM>
</ROOT>

Nope, this isn't what XSD is designed to accomplish. If you're trying to define what the <ITEM> object looks like from a data perspective in XSD, it's not related to any other <ITEM> object. So, each one can only be describe atomically.

The only way I could think to do that in XSD is if the <ITEM> that has <ITEM_BOOLEAN>Y</ITEM_BOOLEAN>, is and always is the FIRST <ITEM> in your list of items, then it could be done. You could design the <ROOT> element to have 2 <ITEM> nodes as allowed children. You define the first <ITEM> as something like "ItemYesType" and it is required and there can be only 1. Then you have a "ItemNoType" which is optional and 0 to many in cardinality. Then you've basically got 2 separate schema objects, one that requries "Y" and one that requires "N".

The other way to validate this type of message is with XSLT. You can very easily write a transformation to check for this constraint. If it fails the constraint, you throw an error and stop the message with and error. We do in our house for complicated business rules that XSD cannot validate (Exactly like yours). We put a message through XSD Validation, then we push it into an XSLT constraint check immediately after.

commented: Helpful and direct +0

Iceandrews,

Thank you very much for your reply. I suspected that this would be the case. Thank you for pointing me in the directions of XSLT files. While the validation I'm doing is beyond the scope of my project I prefer to leave no stone unturned. In the future I will know the answer lies in XSLT files for business rule validation. Thank you so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.