10

I believe this has to do with keyref but I'm not for sure, and I am really not sure that it can be done at all.

For example, say I have myElement1 and myElement2. If there are no myElement2 in the XML file, then myElement1 must exist, otherwise it is optional.

Is there any way to force this type of validation in my XSD file?

Rookie Programmer Aravind
  • 11,952
  • 23
  • 81
  • 114
hmcclungiii
  • 1,765
  • 2
  • 16
  • 27
  • 1
    I'm pretty sure that the answer is not because of ambiguity: the schema for the container of such elements would have two diferents definitions. The only solution is to declare the "optional/required" element to be optional and to use a second phase validation. –  Nov 08 '10 at 22:36

2 Answers2

27

Similar questions have been asked several times .. in stackoverflow or any tech blogs ..

but the answer is always not possible,


cases like :

(a) validating Element depending on the value or presence of any other element/attribute
(b) validating value of an element depending on the value or presence of any other element/attribute
(c) validating attribute depending on the value or presence of any other element/attribute
(d) defining elements/attributes of same name having different Types/DataTypes under same scope
(e) validating child elements on the basis of value or presence of any other Elements/Attributes

all the above mentioned cases are not possible to achieve ..

but if you can implement some transformation thing .. then you can get help. what I mean to say is "first go through transformation and modify the XML as per your requirement,

example :: if the particular field is expected with some value .. pass the XML as it is if the condition is satisfied and don't pass if it doesn't .. in the next block XSD is going to validate and obviously errors out :) you can figure out that it is missing your custom validation ..

anyway the intention is to block or Pass the xml according to the data .. and that is what you are doing ..

Note: Schematron has been come into good practice recently! Here is the tutorial: http://zvon.org/xxl/SchematronTutorial/General/contents.html

Rookie Programmer Aravind
  • 11,952
  • 23
  • 81
  • 114
  • Thanks for the info! I searched before posting but couldn't really find an answer. Basically, I discovered that it is impossible to do just with validation and schema. – hmcclungiii Nov 15 '10 at 23:42
  • 1
    This is a reasonably but not completely accurate paraphrase of the situation in XSD 1.0: cases (a), (b), (e), and (f) are sometimes possible, depending on the details of the desired constraint. And in XSD 1.1 the conditional-type and assertion features make a somewhat broader range of constraints expressible. – C. M. Sperberg-McQueen Mar 04 '13 at 19:14
  • @C.M.Sperberg-McQueen, as per my understanding .. in XSD 1.0 a,b,e,f are impossible to achieve (if you have `choice` in your mind, then it has its own restriction). If you know examples for a,b,e,f please provide me I am interested to know (as you say sometimes, I am curious about the extent) Thank you for your valuable comments – Rookie Programmer Aravind Mar 04 '13 at 23:46
  • clause (e): you cannot have elements/attr with same name and different type under same hierarchy! while it can appear in different hierarchy with different complexType or simpleType or just type.. (in which case, those elements/attr are treated as two independent but not the same ones) – Rookie Programmer Aravind Mar 04 '13 at 23:49
  • 1
    @InfantPro'Aravind' - cases (a), (b), (c), and (f) all include possible dependencies on the *presence* of another element, as well as its value. But content models make it easy for element X to be required (or optional, or legal) if and only if element Y is present: the model `(Y, X)?` (for example) makes child X required if Y is present, and illegal otherwise. And local bindings make it possible for the type of an element to depend not just on its parent but on any ancestor. XSD 1.0 cannot support dependency of these kinds on element *values*, but *presence* of elements is another matter. – C. M. Sperberg-McQueen Mar 05 '13 at 00:16
1

What I would suggest is to create XSD for each cases defined below:

Cases like:

  1. Validating Element depending on the value or presence of any other element/attribute.
  2. Validating value of an element depending on the value or presence of any other element/attribute.
  3. Validating attribute depending on the value or presence of any other element/attribute.
  4. Ignoring (random) elements/attributes from validation isn't allowed. (I mean to say is usage of has restrictions.)
  5. Elements/attributes of same name having different DataTypes.
  6. Validating child elements on the basis of value or presence of any other Elements/Attributes.

And create there own XML for that.

Kijewski
  • 25,517
  • 12
  • 101
  • 143