1

我有一个如下所示的 XML 文件。我正在使用它向机器人发出一系列命令:

<Task StartPosition="100,100">
      <GoTo>
        <X>100</X>
        <Y>200</Y>
      </GoTo>
      <MoveForward>
        <Distance>50</Distance><!--cm-->
      </MoveForward>
      <Rotate Direction="clockwise" Time="2">
        <Degrees>60</Degrees>
      </Rotate>
      <GoTo>
        <X>200</X>
        <Y>300</Y>
      </GoTo>
      <Rotate Direction="clockwise">
        <Degrees>120</Degrees>
      </Rotate>
      <SoundRecord>
        <Time>5</Time>
      </SoundRecord>
      <SoundPlayback>
        <Time>5</Time>
      </SoundPlayback>
    </Task>

如您所见,Task 元素具有相同类型的子元素,它们不会一个接一个地放置,例如 GoTo 元素。我使用 Microsoft Visual Studio 命令提示符 (2010) 中的 xsd.exe 根据上面的 XML 文件生成此架构:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Task">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="GoTo" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="X" type="xs:string" minOccurs="0" />
              <xs:element name="Y" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="MoveForward" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Distance" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="Rotate" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Degrees" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
            </xs:sequence>
            <xs:attribute name="Direction" type="xs:string" />
            <xs:attribute name="Time" type="xs:string" />
          </xs:complexType>
        </xs:element>
        <xs:element name="SoundRecord" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Time" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="SoundPlayback" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Time" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="StartPosition" type="xs:string" />
    </xs:complexType>
  </xs:element>
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="Task" />
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

我正在使用以下代码验证我的 XML 文件,包括此示例中针对此架构的文件:

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, xsdFilePath);
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

// Create the XmlReader object.
XmlReader reader = XmlReader.Create(xmlFilePath, settings);

// Parse the file to validate it
while (reader.Read());

\**************************************************\

private static void ValidationCallBack(object sender, ValidationEventArgs args)
{
    if (args.Severity == XmlSeverityType.Warning)
        throw new Exception("\tWarning: Matching schema not found.  No validation     occurred." + args.Message);
    else
        throw new Exception("\tValidation error: " + args.Message);    
}

我的问题是我总是收到以下消息的验证错误:

验证错误:元素“任务”具有无效的子元素“GoTo”。预期的可能元素列表:“旋转、声音记录、声音播放”。

您是否知道一种方法可以根据我的模式验证我的 XML 文件,该模式只会检查是否存在正确的元素类型但不关心顺序?或者您知道我是否可以更改架构以使 XML 文件通过验证?或者我的 XML 格式是一种不好的做法,它无法通过模式验证?:)

我真的很感激任何帮助。谢谢

4

1 回答 1

1

We can replace the xs:sequence, in which the values are required to be in a particular order, with an unlimited xs:choice which should achieve the desired result.

Try this. Note I have replaced <xs:sequence> with <xs:choice>. Note also the attributes which allow as many selections as you want.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Task">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded"><!-- Here is the change -->
        <xs:element name="GoTo" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="X" type="xs:string" minOccurs="0" />
              <xs:element name="Y" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="MoveForward" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Distance" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="Rotate" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Degrees" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
            </xs:sequence>
            <xs:attribute name="Direction" type="xs:string" />
            <xs:attribute name="Time" type="xs:string" />
          </xs:complexType>
        </xs:element>
        <xs:element name="SoundRecord" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Time" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="SoundPlayback" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Time" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
      <xs:attribute name="StartPosition" type="xs:string" />
    </xs:complexType>
  </xs:element>
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="Task" />
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>
于 2013-05-11T08:04:27.363 回答