tags:

views:

174

answers:

1

I have the following XSD:

<xs:complexType name="typeBroken">
  <xs:choice>
    <xs:element name="B">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:pattern value="FOO|BAR" />
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    <xs:sequence>
      <xs:element name="A">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:maxLength value="5" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="B">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:maxLength value="3" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
    </xs:sequence>
  </xs:choice>
</xs:complexType>

So, I would like the presence of 'A' to make 'B' to have a different validation. Is this possible? For example:

<test><B>FOO</B></test>
<test><A>HELLO</A><B>BAZ</B><test>

Should both validate. While:

<test><B>BAZ</B></test>

Should NOT validate. However, I am getting from xsd:

cos-element-consistent: Error for type 'typeBroken'. Multiple elements with name 'B', with different types, appear in the model group.
A: 

Do I understand your requirements correctly? You want to have a <B>...</B> and optionally an <A>....</A> before that (but not required)?

How about this schema then?

<xs:complexType name="typeBroken">
  <xs:sequence>
    <xs:element name="A" minOccurs="0">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:maxLength value="5" />
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    <xs:element name="B">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:pattern value="FOO|BAR" />
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    </xs:sequence>
  </xs:choice>
</xs:complexType>

Define a sequence where the first element, <A>, is optional (minOccurs="0") while the second one is not optional.

Does that solve your requirement?

Marc

marc_s
A is optional, yes, but the restrictions on B vary depending on the presence (or not) of the A element. On your solution, B can only be FOO|BAR regardless of A. I would like B to have a different validation if A is present. - Victor
Ah, okay - I'm afraid, such a validation cannot be expressed in XSD.
marc_s