Possible to create XSD for checking element interdependecies?

Discussion in 'XML' started by ranma79, Aug 29, 2003.

  1. ranma79

    ranma79 Guest

    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!
    ranma79, Aug 29, 2003
    #1
    1. Advertising

  2. ranma79

    Bob Foster Guest

    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

    "ranma79" <> wrote in message
    news:...
    > 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!
    Bob Foster, Aug 30, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,081
    Markus
    Nov 22, 2005
  2. Markus
    Replies:
    1
    Views:
    457
    Markus
    Nov 22, 2005
  3. Matthet
    Replies:
    2
    Views:
    662
    Matthet
    Sep 24, 2003
  4. PeteOlcott
    Replies:
    1
    Views:
    934
    Mukul Gandhi
    Feb 5, 2009
  5. Iulian Ilea

    problem: create element in create element

    Iulian Ilea, Dec 14, 2006, in forum: Javascript
    Replies:
    4
    Views:
    176
    Iulian Ilea
    Dec 17, 2006
Loading...

Share This Page