5

作为一名拥有大量 XML 消费和生产经验的开发人员,我以前从未真正与模式进行过交互。这是我第一次真正发生这种情况。

我遇到了一个“功能”,我认为它更像是一个有据可查的错误。

使用 XDocument.Validate() 时,似乎在某些情况下,如果文档与指定的架构不匹配,则文档将有效。我觉得这很可能是我对 XSD、XML 名称空间和预期验证过程之间关系的理解存在缺陷。

因此,我向您提交了我的 XML 示例、我的 XSD 示例和我的验证代码。

XML - 这是故意错误的文档。

<?xml version="1.0" encoding="utf-8" ?>
<SuppliesDefinitions 
  xmlns="http://lavendersoftware.org/schemas/SteamGame/Data/Xml/Supplies.xsd">
  <Supply type="Common">
    <Information/>
    <Ritual/>
    <Weapon/>
    <Tool count="1"/>
    <Tool count="2"/>
    <Tool count="3"/>
  </Supply>
  <Supply type="Uncommon">
    <Information/>
    <Weapon/>
    <Tool count="1"/>
    <Tool count="2"/>
    <Tool count="3"/>
    <Tool count="4"/>
  </Supply>
  <Supply type="Rare">
    <Information/>
    <Rune/>
    <Weapon/>
    <Tool count="2"/>
    <Tool count="3"/>
    <Tool count="4"/>
  </Supply>
</SuppliesDefinitions>

XSD 用于验证它。(同样,这故意是上述 XML 的错误文档)

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Encounters"
    targetNamespace="http://lavendersoftware.org/schemas/SteamGame/Data/Xml/Encounters.xsd"
    elementFormDefault="qualified"
    xmlns="http://lavendersoftware.org/schemas/SteamGame/Data/Xml/Encounters.xsd"
    xmlns:mstns="http://lavendersoftware.org/schemas/SteamGame/Data/Xml/Encounters.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:complexType name="ToolType">
    <xs:attribute name="count" use="required" type="xs:int"/>
  </xs:complexType>

  <xs:complexType name="TaskType">
    <xs:choice maxOccurs="unbounded" minOccurs="1">
      <xs:element name="Weapon"/>
      <xs:element name="Information"/>
      <xs:element name="Tool" type="ToolType"/>
      <xs:element name="Ritual"/>
    </xs:choice>
  </xs:complexType>


  <xs:complexType name="EncounterType">
    <xs:sequence maxOccurs="unbounded" minOccurs="1">
      <xs:element name="Task" type="TaskType"/>
    </xs:sequence>
    <xs:attribute name="name" use="required" type="xs:string"/>
  </xs:complexType>

  <xs:element name="EncounterDefinitions">
    <xs:complexType>
      <xs:sequence maxOccurs="unbounded" minOccurs="1">
        <xs:element name="Encounter" type="EncounterType"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

最后是验证码。

    private static void ValidateDocument(XDocument doc)
    {
        XmlSchemaSet schemas = new XmlSchemaSet();
        schemas.Add(null, XmlReader.Create(new StreamReader(XmlSchemaProvider.GetSchemaStream("Encounters.xsd"))));

        doc.Validate(schemas, (o, e) =>
        {
            //This is never hit!
            Console.WriteLine("{0}", e.Message);
            Assert.False(e.Severity == XmlSeverityType.Error);
        });
    }

我想知道是否有人可以解释我做错了什么。我觉得我对这应该工作的方式做出了一些不正确的假设。在我看来,对一个完全不相关的 XML 文档使用一个 xsd 是无效的。

4

1 回答 1

4

There is no nodes in your XML that can be validated by the schema (namespaces are different). As result it does not report any errors. As far as I know behavior for nodes that are not matched to any schema is allow anything.

You also could set validation options in XmlReaderSettings to allow warnings:

ReportValidationWarnings - Indicates that events should be reported if a validation warning occurs. A warning is typically issued when there is no DTD or XML Schema to validate a particular element or attribute against. The ValidationEventHandler is used for notification.

Check out XmlSchemaSet.Add and HOW TO: Validate an XML Document by Using Multiple Schemas if you expect nodes from multiple namespaces to be present in the XML.

于 2013-05-26T01:08:58.837 回答