如何更正XSD以便XML验证?

时间:2015-06-18 15:31:47

标签: xml xsd xml-validation

在我的应用程序中,我有如下所示的XSD。这些是我的应用程序中现有XSD的修改,它们是我的出发点。

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>

我还有一个示例XML文件,它是修改后的XSD必须描述的格式。

<?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>

XML无法验证:我收到的消息是:

  

命名空间'aNamespace'中的元素'read'在命名空间'aNamespace'中有一个无效的子元素'imagedata'。预期可能元素的列表:名称空间'aNamespace'中的'plate,confidence,overviews,gps'。

如何修复XSD以便xml验证?

1 个答案:

答案 0 :(得分:1)

只需将imagedata元素的全局声明添加到Snapshot.xsd:

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

然后在需要的两个地方引用它:

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

您的XML将成功验证。

以下是两个完整的XSD改变......

<强> 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>