Possible to create XSD for checking element interdependecies?

R

ranma79

Hi,

I was wondering if anyone knows if this is possible. I have the
following simple xml document as follows:

<test>
<field1>Value1</field1>
<field2>Value2</field2>
</test>

I would like to create an XSD which validates that if the "field1"
element contains a value, then "field2" must also contain a value. And
also the other way around, if "field2" contains a value, then "field1"
must contain a value. However, if both fields do not contain values
then no validation errors occur. Any ideas? Thanks!
 
B

Bob Foster

Can't do it as stated. You can't have the type of one element (empty or not)
depend on the type of another, or any other test that amounts to the same
thing.

What you can do is require that if field1 is specified then field2 must be
specified, and vice versa. Since you can require that field1 and field2 have
values, this amounts to the same thing. One such schema fragment is:

<element name="test">
<complexType>
<complexContent>
<sequence minOccurs="0">
<element name="field1" type="integer"/>
<element name="field2" type="integer"/>
</sequence>
</complexContent>
</complexType>
</element>

(You would have no problem writing a schema per your original problem
statement in RELAX NG.)

Bob Foster
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top