title: How to control references between elements in XML
body: <p>In my previous question (<a href="https://stackoverflow.com/questions/29365082/how-create-references-between-elements-in-xml">How create references between elements in XML</a>)  I asked how to create, in an XML Schema, references between elements.</p>
  
  <p>Ok, but now I noticed something. For example, defining these three types of vehicles:</p>
  
  <pre><code>&lt;Car id="car1"&gt;
      &lt;plate&gt;AAA&lt;/plate&gt;
      &lt;mark&gt;Peugeot&lt;/mark&gt;
      &lt;model&gt;206&lt;/model&gt;
  &lt;/Car&gt;
  
  &lt;Truck id="truck1"&gt;
      &lt;plate&gt;BBB&lt;/plate&gt;
      &lt;mark&gt;Scania&lt;/mark&gt;
      &lt;model&gt;X1&lt;/model&gt;
  &lt;/Truck&gt;
  
  &lt;Trailer id="trailer1"&gt;
      &lt;plate&gt;CCC&lt;/plate&gt;
      &lt;mark&gt;Scania&lt;/mark&gt;
      &lt;model&gt;T1&lt;/model&gt;
  &lt;/Trailer&gt;
  </code></pre>
  
  <p>... I can now define <em>complex vehicle</em>, for example:</p>
  
  <pre><code>&lt;TrailerTruck id="tt1"&gt;
      &lt;Car refid="car1"/&gt;
      &lt;Trailer refid="trailer1"/&gt;
  &lt;/TrailerTruck&gt;
  
  &lt;TrailerTruck id="tt2"&gt;
      &lt;Truck refid="truck1"/&gt;
      &lt;Trailer refid="trailer1"/&gt;
  &lt;/TrailerTruck&gt;
  </code></pre>
  
  <p>Okay, it works, but I noticed that - unfortunately - also allowed such a thing:</p>
  
  <pre><code>&lt;TrailerTruck id="tt3_WRONG"&gt;
      &lt;Truck refid="trailer1"/&gt;   &lt;!-- an element "Truck" should not
      refer to a trailer!! Should be able to refer only to truck1 --&gt;
      &lt;Trailer refid="car1"/&gt;     &lt;!-- like before, an element Trailer
      should be able to refer only trailer1 --&gt;
  &lt;/TrailerTruck&gt;
  
  &lt;TrailerTruck id="tt4_WRONG"&gt;
      &lt;Car refid="truck1"/&gt;       &lt;!-- an element **Car** should not
      refer to a Truck !! --&gt;
      &lt;Trailer refid="trailer1"/&gt;
  &lt;/TrailerTruck&gt;
  </code></pre>
  
  <p>So, if I put incorrect references, is not shown any error. Instead, I would like to be notified. I want to insert some kind of restriction or control. But how?</p>
  
  <hr>
  
  <p>Now I show you my current implementation.</p>
  
  <p><strong>VehicleXMLSchema.xsd</strong>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  
      &lt;xs:element name="Fleet" type="FleetType"/&gt;
  
  
      &lt;xs:complexType name="FleetType"&gt;
          &lt;xs:choice maxOccurs="unbounded"&gt;
              &lt;xs:element ref="Car" /&gt;
              &lt;xs:element ref="Truck" /&gt;
              &lt;xs:element ref="Trailer" /&gt;
              &lt;xs:element ref="TrailerTruck" /&gt;
          &lt;/xs:choice&gt;
      &lt;/xs:complexType&gt;
  
  
      &lt;xs:complexType name="VehicleType" abstract="true"&gt;
          &lt;xs:sequence minOccurs="0"&gt;
              &lt;xs:element name="plate" type="xs:string" minOccurs="1" /&gt;
              &lt;xs:element name="mark" type="xs:string" minOccurs="1" /&gt;
              &lt;xs:element name="model" type="xs:string" minOccurs="1" /&gt;
          &lt;/xs:sequence&gt;
      &lt;/xs:complexType&gt;
  
  
  
  
      &lt;!-- SimpleVehicle and ComplexVehicle --&gt;
  
      &lt;xs:complexType name="SimpleVehicle"&gt;
          &lt;xs:complexContent&gt;
              &lt;xs:extension base="VehicleType"&gt;
                  &lt;xs:attribute name="id" type="xs:ID"/&gt;
              &lt;/xs:extension&gt;
          &lt;/xs:complexContent&gt;
      &lt;/xs:complexType&gt;
  
      &lt;xs:complexType name="ComplexVehicle"&gt;
          &lt;xs:complexContent&gt;
              &lt;xs:extension base="VehicleType"&gt;
                  &lt;xs:attribute name="refid" type="xs:IDREF"/&gt;
              &lt;/xs:extension&gt;
          &lt;/xs:complexContent&gt;
      &lt;/xs:complexType&gt;
  
  
  
  
      &lt;!-- Car, Truck, Trailer --&gt;
  
      &lt;xs:element name="Car" type="SimpleVehicle" /&gt;
      &lt;xs:element name="Truck" type="SimpleVehicle"/&gt;
      &lt;xs:element name="Trailer" type="SimpleVehicle"/&gt;
  
  
  
  
      &lt;!-- TrailerTruck --&gt;
  
      &lt;xs:group name="DrivingPart"&gt;
          &lt;xs:choice&gt;
              &lt;xs:element name="Car" type="ComplexVehicle" /&gt;
              &lt;xs:element name="Van" type="ComplexVehicle"/&gt;
              &lt;xs:element name="Truck" type="ComplexVehicle"/&gt;
          &lt;/xs:choice&gt;
      &lt;/xs:group&gt;
  
      &lt;xs:element name="TrailerTruck"&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                  &lt;xs:group ref="DrivingPart" minOccurs="1" maxOccurs="1"/&gt;
                  &lt;xs:element name="Trailer" type="ComplexVehicle" minOccurs="1" maxOccurs="1"/&gt;
              &lt;/xs:sequence&gt;
              &lt;xs:attribute name="id" type="xs:ID"/&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p><strong>Shipper1.xml</strong>:</p>
  
  <pre><code>&lt;Fleet shipperName="Shipper1" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="VehicleXMLSchema.xsd"&gt;
  
      &lt;Car id="car1"&gt;
          &lt;plate&gt;AAA&lt;/plate&gt;
          &lt;mark&gt;Peugeot&lt;/mark&gt;
          &lt;model&gt;206&lt;/model&gt;
      &lt;/Car&gt;
  
      &lt;Truck id="truck1"&gt;
          &lt;plate&gt;DDD&lt;/plate&gt;
          &lt;mark&gt;Scania&lt;/mark&gt;
          &lt;model&gt;&lt;/model&gt;
      &lt;/Truck&gt;
  
      &lt;Trailer id="trailer1"&gt;
          &lt;plate&gt;EEE&lt;/plate&gt;
          &lt;mark&gt;Scania&lt;/mark&gt;
          &lt;model&gt;&lt;/model&gt;
      &lt;/Trailer&gt;
  
  
      &lt;TrailerTruck id="trailerTruck1"&gt;
          &lt;Car refid="car1" /&gt;
          &lt;Trailer refid="trailer1" /&gt;
      &lt;/TrailerTruck&gt;
  
      &lt;TrailerTruck id="trailerTruck2_WRONG"&gt;
          &lt;Truck refid="car1" /&gt;
          &lt;Trailer refid="trailer1" /&gt;
      &lt;/TrailerTruck&gt;
  
      &lt;TrailerTruck id="trailerTruck3_WRONG"&gt;
          &lt;Truck refid="truck1" /&gt;
          &lt;Trailer refid="car1" /&gt;
      &lt;/TrailerTruck&gt;
  
  &lt;/Fleet&gt;
  </code></pre>