How to correct XSD so XML validates?

183 views Asked by At

In my application, I have the XSDs shown below. These are modifications of existing XSDs in my application and they are my starting point.

GPSInformation.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="aNamespace" elementFormDefault="qualified" targetNamespace="aNamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="gps">
    <xs:annotation>
      <xs:documentation>An instance of this class contains all of the information we want to retrieve from the GPS device.</xs:documentation>
    </xs:annotation>
    <xs:sequence>
       <xs:element minOccurs="0" maxOccurs="1" name="position" type="gpsposition">
        <xs:annotation>
          <xs:documentation>The determined position of the GPS receiver.</xs:documentation>
        </xs:annotation>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="gpsposition">
    <xs:annotation>
      <xs:documentation>This class represents the GPS coordinates read from the GPS device that is connected to the computer.  An instance of the class represents the position information returned from the GPS device when it is queried for a position.</xs:documentation>
    </xs:annotation>
    <xs:attribute name="lat" use="required">
      <xs:annotation>
        <xs:documentation>The current position's latitude, in decimal degrees.  Range is -90.000 to +90.000.</xs:documentation>
      </xs:annotation>
      <xs:simpleType>
        <xs:restriction base="xs:double">
          <xs:minInclusive value="-90"/>
          <xs:maxInclusive value="90"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="long" use="required">
      <xs:annotation>
        <xs:documentation>The current position's longitude, in decimal degrees.  Range is +180.000 to -180.000.</xs:documentation>
      </xs:annotation>
      <xs:simpleType>
        <xs:restriction base="xs:double">
          <xs:minInclusive value="-180"/>
          <xs:maxInclusive value="180"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
  </xs:complexType>
</xs:schema>

Snapshot.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="aNamespace" elementFormDefault="qualified" targetNamespace="aNamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="snapshot">
    <xs:annotation>
      <xs:documentation>One snapshot taken by a camera in the system.</xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="imagedata" type="xs:base64Binary">
        <xs:annotation>
          <xs:documentation>The binary image representation.</xs:documentation>
        </xs:annotation>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="camera" type="xs:string">
      <xs:annotation>
        <xs:documentation>The ID or name of the camera that captured this image.</xs:documentation>
      </xs:annotation>
    </xs:attribute>
  </xs:complexType>
</xs:schema>

Read.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="aNamespace" elementFormDefault="qualified" targetNamespace="aNamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:include schemaLocation="Snapshot.xsd"/>
    <xs:include schemaLocation="GPSInformation.xsd"/>
    <xs:simpleType name="guid">
        <xs:restriction base="xs:string">
            <xs:pattern value="[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}" />
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="read" type="read"/>
    <xs:complexType name="read">
        <xs:annotation>
            <xs:documentation>Represents a read generated from an LPR camera.</xs:documentation>
        </xs:annotation>
        <xs:complexContent mixed="false">
            <xs:extension base="snapshot">
                <xs:sequence>
                    <xs:element minOccurs="1" maxOccurs="1" name="timestamp" type="xs:dateTime">
                        <xs:annotation>
                            <xs:documentation>The timestamp of the time this item was generated.</xs:documentation>
                        </xs:annotation>
                    </xs:element>
                    <xs:element minOccurs="0" maxOccurs="1" name="plate" type="xs:string">
                        <xs:annotation>
                            <xs:documentation>The OCR of the plate read.</xs:documentation>
                        </xs:annotation>
                    </xs:element>
                    <xs:element minOccurs="0" maxOccurs="1" name="confidence" type="xs:int">
                        <xs:annotation>
                            <xs:documentation>The confidence level of the OCR. Usually from 1 to 100.</xs:documentation>
                        </xs:annotation>
                    </xs:element>
                    <xs:element minOccurs="0" maxOccurs="1" name="overviews">
                        <xs:annotation>
                            <xs:documentation>All secondary images associated with this item.</xs:documentation>
                        </xs:annotation>
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element minOccurs="0" maxOccurs="unbounded" name="snapshot" type="snapshot"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                    <xs:element minOccurs="0" maxOccurs="1" name="gps" type="gps">
                        <xs:annotation>
                            <xs:documentation>The GPS information of the reader when this read was taken.</xs:documentation>
                        </xs:annotation>
                    </xs:element>
                </xs:sequence>
                <xs:attribute name="id" type="guid" use="required">
                    <xs:annotation>
                        <xs:documentation>A unique ID to represent this read.</xs:documentation>
                    </xs:annotation>
                </xs:attribute>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>

Main.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="aNamespace" elementFormDefault="qualified" targetNamespace="aNamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:include schemaLocation="Read.xsd" />
  <xs:element name="reads" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msdata:Prefix="x">
    <xs:complexType>
      <xs:annotation>
        <xs:documentation>Contains one or more reads generated from one or more LPR cameras.</xs:documentation>
      </xs:annotation>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="read" msdata:Prefix="x" type="read" minOccurs="0" maxOccurs="unbounded">
          <xs:annotation>
            <xs:documentation>Contains information for 0 or more reads.</xs:documentation>
          </xs:annotation>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

I also have an example XML file, which is the format that the modified XSDs have to describe.

<?xml version="1.0" encoding="utf-8"?>
<x:reads xmlns:x="aNamespace">
  <x:read camera="Right" id="0a92adbb-01dd-4a85-ae1b-d420e30d6896">
    <x:timestamp>2015-06-12T16:47:04.1470000-04:00</x:timestamp>
    <x:imagedata>a</x:imagedata>
    <x:plate>TAXSCOFF</x:plate>
    <x:confidence>95</x:confidence>
    <x:overviews>
      <x:snapshot camera="Right">
        <x:imagedata></x:imagedata>
      </x:snapshot>
    </x:overviews>
    <x:gps>
      <x:position lat="41.366743911028792" long="-73.613886014875149" />
    </x:gps>
  </x:read>
  <x:read camera="Right" id="0c5ee809-c6ce-4866-af2c-d7d561b99263">
    <x:timestamp>2015-06-12T16:43:46.1880000-04:00</x:timestamp>
    <x:imagedata>a</x:imagedata>
    <x:plate>WANTEDPE</x:plate>
    <x:confidence>87</x:confidence>
    <x:overviews>
      <x:snapshot camera="Right">
        <x:imagedata></x:imagedata>
      </x:snapshot>
    </x:overviews>
    <x:gps>
      <x:position lat="41.366748955299791" long="-73.613680088554659" />
    </x:gps>
  </x:read>
</x:reads>

The XML does not validate: The message I'm getting is:

The element 'read' in namespace 'aNamespace' has an invalid child element 'imagedata' in namespace 'aNamespace'. List of possible elements expected: 'plate, confidence, overviews, gps' in namespace 'aNamespace'.

How do I fix my XSDs so the xml validates?

1

There are 1 answers

0
kjhughes On BEST ANSWER

Simply add a global declaration of the imagedata element to Snapshot.xsd:

  <xs:element name="imagedata" type="xs:string">
    <xs:annotation>
      <xs:documentation>The binary image
      representation.</xs:documentation>
    </xs:annotation>
  </xs:element>

And then reference it in the two places it's needed:

      <xs:element minOccurs="0" maxOccurs="1" ref="imagedata"/>

And your XML will validate successfully.

Here are the two changed XSDs in their entirety...

Snapshot.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="aNamespace"
           elementFormDefault="qualified"
           targetNamespace="aNamespace"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="imagedata" type="xs:string">
    <xs:annotation>
      <xs:documentation>The binary image
      representation.</xs:documentation>
    </xs:annotation>
  </xs:element>
  <xs:complexType name="snapshot">
    <xs:annotation>
      <xs:documentation>One snapshot taken by a camera in the
      system.</xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" ref="imagedata"/>
    </xs:sequence>
    <xs:attribute name="camera" type="xs:string">
      <xs:annotation>
        <xs:documentation>The ID or name of the camera that captured
        this image.</xs:documentation>
      </xs:annotation>
    </xs:attribute>
  </xs:complexType>
</xs:schema>

Read.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="aNamespace"
           elementFormDefault="qualified"
           targetNamespace="aNamespace"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:include schemaLocation="Snapshot.xsd"/>
  <xs:include schemaLocation="GPSInformation.xsd"/>
  <xs:simpleType name="guid">
    <xs:restriction base="xs:string">
      <xs:pattern value="[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}" />
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="read" type="read"/>
  <xs:complexType name="read">
    <xs:annotation>
      <xs:documentation>Represents a read generated from an LPR
      camera.</xs:documentation>
    </xs:annotation>
    <xs:complexContent mixed="false">
      <xs:extension base="snapshot">
        <xs:sequence>
          <xs:element minOccurs="1" maxOccurs="1"
                      name="timestamp" type="xs:dateTime">
            <xs:annotation>
              <xs:documentation>The timestamp of the time this item
              was generated.</xs:documentation>
            </xs:annotation>
          </xs:element>

          <xs:element minOccurs="0" maxOccurs="1" ref="imagedata"/>

          <xs:element minOccurs="0" maxOccurs="1" name="plate" type="xs:string">
            <xs:annotation>
              <xs:documentation>The OCR of the plate read.</xs:documentation>
            </xs:annotation>
          </xs:element>
          <xs:element minOccurs="0" maxOccurs="1" name="confidence" type="xs:int">
            <xs:annotation>
              <xs:documentation>The confidence level of the
              OCR. Usually from 1 to 100.</xs:documentation>
            </xs:annotation>
          </xs:element>
          <xs:element minOccurs="0" maxOccurs="1" name="overviews">
            <xs:annotation>
              <xs:documentation>All secondary images associated with
              this item.</xs:documentation>
            </xs:annotation>
            <xs:complexType>
              <xs:sequence>
                <xs:element minOccurs="0" maxOccurs="unbounded"
                            name="snapshot" type="snapshot"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element minOccurs="0" maxOccurs="1" name="gps" type="gps">
            <xs:annotation>
              <xs:documentation>The GPS information of the reader when
              this read was taken.</xs:documentation>
            </xs:annotation>
          </xs:element>
        </xs:sequence>
        <xs:attribute name="id" type="guid" use="required">
          <xs:annotation>
            <xs:documentation>A unique ID to represent this
            read.</xs:documentation>
          </xs:annotation>
        </xs:attribute>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>