0

I have 3 kinds of XML blocks, containing no "filter" block, or "filter" with "invalidation" whose first value is either "fixed1" or "fixed2"; if "fixed1": nothing else, if "fixed2", this sequence A1,A2,A3 (see below) :

        <filter>
          <invalidation>fixed1</invalidation>
        </filter>

        <filter>
          <invalidation>fixed2</invalidation>
          <A1>string...</A1>
          <A2>string...</A2>
          <A2>string...</A3>
        </filter>

How to validate this ? I feel that there is some solution using xs:choice, but I couldn't find.

The only (poor) solution I found is :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
<xs:simpleType name="invalidation">
  <xs:restriction base="xs:string">
    <xs:pattern value="fixed1|fixed2"/>
  </xs:restriction>
</xs:simpleType>
...
<xs:element name="filter" minOccurs="0">
  <xs:complexType>
    <xs:all>
      <xs:element name="invalidation" type="invalidation"/>
      <xs:element name="A1" type="xs:string" minOccurs="0"/>
      <xs:element name="A2" type="xs:string" minOccurs="0"/>
      <xs:element name="A3" type="xs:string" minOccurs="0"/>
    </xs:all>
  </xs:complexType>
</xs:element>
Eric H.
  • 2,152
  • 4
  • 22
  • 34
  • try it with a `´ see: http://stackoverflow.com/questions/24774554/xsd-1-1-xsalternative-xsassert – Jérôme Mar 29 '17 at 12:13
  • 1
    @Jérôme, xs:alternative only works if the selection of a type is based on attribute values, it can't be based on the value of the first element. – Michael Kay Mar 29 '17 at 14:59

1 Answers1

1

This cannot be done in XSD 1.0.

It can be done in XSD 1.1 using assertions. Define the structure as a sequence with the content model (invalidation, (A1, A2, A3)?) and then define an assertion: not(invalidation='fixed1' and exists(A1)).

XSD 1.1 is supported in Altova, Saxon, and Apache Xerces, but not in other products such as the built-in validators in Java and .NET.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164