views:

201

answers:

2

Is it possible to define in XSD the following scenario:

  1. Parent element has an attribute that is optional.
  2. If the attribute is not present in XML, at least one child element must exists.
  3. If the attribute is present, there can be zero or more child elements.

Example:

VALID

<parent external-source="some_name" />

<parent external-source="some_name">
  <child>some value</child>
</parent>

<parent>
  <child> some value</child>
</parent>

NOT VALID

<parent />
+1  A: 

No i think not.

affan
Answers of such type are not satisfactory for me. What is the reasoning behind your statement?
AOI Karasu
XML schema types are very similar to you OO programming types. You can create a complex type and you can restrict order of elements type of element and attribute and restriction on what kind of values can be assigned. That is all xsd can do for you. What you want is something of constraint that span more than one element/attribute which is simply not possible with xsd. you have to write your own code to valid this.
affan
I also think it is not possible, and that saying that is a pretty good answer (if it's correct) :-)
Yossi Dahan
@affan: Thank you very much for your comment. Now I get the picture :)
AOI Karasu
A: 

No .. the reason is : In your case you are trying to validate the presence of an element/tag depending on the value of some other tag/attribute .. (XSD is basically a set of declaration) which requires multiple declaration of a same element ..
Multiple declaration of a same element isn't allowed in XSD .. :-(
Check out the similar problem (click here) posted by a stackOverFlow member

infant programmer