954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

XSD/XML Restriction of Element Value Occurance

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

Scenario: I have an element which has three child elements:

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

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

What I want to validate: Only one occurance of "Y" in <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 <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>

Learning_Curve
Newbie Poster
5 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

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

The only way I could think to do that in XSD is if the that has Y, is and always is the FIRST in your list of items, then it could be done. You could design the element to have 2 nodes as allowed children. You define the first 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.

iceandrews
Junior Poster
185 posts since May 2010
Reputation Points: 10
Solved Threads: 30
 

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.

Learning_Curve
Newbie Poster
5 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: