【问题标题】:XML Schema Validation : Cannot find the declaration of root elementXML 模式验证:找不到根元素的声明
【发布时间】:2022-02-08 03:54:17
【问题描述】:

我对 XML 很陌生,尽管我已经管理得很好,但我现在被这个关于 XML Schema 的棘手情况困住了。您可以在下面看到我的 XML 文档代码及其各自的 Schema 代码。首先这里是我的 XML 文档代码。

<?xml version="1.0" encoding="UTF-8"?>
<Cars
  xmlns="http://www.verkkokauppa.com"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema"
  xsi:schemaLocation="http://www.verkkokauppa.com 4Schema.xsd">

<Car>
    <Brand>BMW</Brand>
    <Model>535d</Model>
    <Gear>Automatic</Gear>
    <Year>2007</Year>
    <Info>It's a german.</Info>
</Car>
</Cars>

下面你可以看到我的代表性架构代码

<?xml version="1.0" encoding="UTF-8"?>
  <xsd:schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.verkkokauppa.com"
    xmlns="http://www.verkkokauppa.com" 
    elementFormDefault = "qualified">
  <xsd:element name = "Brand" type = "xsd:string"/>
  <xsd:element name = "Model" type = "xsd:string"/>
  <xsd:element name = "Gear" type = "xsd:string"/>
  <xsd:element name = "Year" type = "xsd:int"/>
  <xsd:element name = "Information" type = "xsd:string"/>
  <xsd:complexType name = "CarType">
      <xsd:sequence>
          <xsd:element ref = "Brand"/>
          <xsd:element ref = "Model"/>
          <xsd:element ref = "Gear"/>
          <xsd:element ref = "Year"/>
          <xsd:element ref = "Information" 
            minOccurs = "0" maxOccurs = "1" /> 
      </xsd:sequence>
  </xsd:complexType>
  <xsd:element name = "Cars">
      <xsd:complexType>
          <xsd:sequence>
              <xsd:element name = "Car" type = "CarType" 
               maxOccurs = "unbounded"/>
          </xsd:sequence>
      </xsd:complexType>
  </xsd:element>
</xsd:schema>    

现在,每当我尝试验证我的 XML 文档代码时,都会出现错误: “cvc-elt.1:找不到元素‘汽车’的声明。[6]”

我认为这与作为我的根元素的 Cars 元素上的命名空间问题有关,但我真的不确定。

如果有人可以指导我,我将不胜感激。

【问题讨论】:

    标签: xml xsd schema


    【解决方案1】:

    您的猜测是正确的,因为无法匹配命名空间是导致此类错误消息的常见原因。但是您的根元素在命名空间http://www.verkkokauppa.com 中具有本地名称Cars,并且命名空间http://www.verkkokauppa.com 的模式文档声明了一个本地名称为Cars 的元素。所以我不认为这是一个命名空间问题。也许我在这里遗漏了一些微妙的东西。

    如果不是命名空间匹配问题,最可能的原因是您的架构验证器未找到架构文档。

    在这种情况下要问的一般问题是:您使用的是什么验证器?你是如何调用它的?你怎么相信你告诉它在哪里可以找到它应该使用的模式文档?

    如果您依赖根元素上的xsi:schemaLocation 属性,那么您是否检查过您的架构文档与文件名4Schema.xsd 下的输入XML 存在于同一目录中?您是否检查过您的验证器是否会接受模式位置提示? (这是一个提示,而不是指令。)您还应该检查以确保您的 xsi:schemaLocation 属性正确。

    在这种特定情况下,您有一个 xsi:schemaLocation 属性,这是常规的,但您已将前缀 xsi 绑定到命名空间 http://www.w3.org/2001/XMLSchema,而不是命名空间 http://www.w3.org/2001/XMLSchema-instance。没有 XSD 验证器会对名为 {http://www.w3.org/2001/XMLSchema}schemaLocation 的属性执行任何操作——如果他们要查找文档内架构信息,他们会查找 {http://www.w3.org/2001/XMLSchema-instance}schemaLocation

    【讨论】:

    • 好建议。现在问题已解决,并通过将“-instance”添加到我的命名空间名称中来解决。在这件事上我还有很多东西要学,但我很高兴我现在可以通过我的练习迈出下一步。非常感谢。
    【解决方案2】:

    您需要了解 XML 验证的基础知识。是的,您最初的猜测是正确的,那就是与 namaspace 相关的问题。除了那个 XML 文档结构是错误的。您已经在 XSD 中定义了 Information 元素,但是在 XML 中定义了 info 元素。如果您是初学者,请通过此tutorial 了解 XML 命名空间。希望以下修改后的代码对您有所帮助。

    在下面找到修改后的代码

    XSD:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="http://www.verkkokauppa.com" 
      xmlns="http://www.verkkokauppa.com" 
      elementFormDefault="qualified">
    <xsd:element name = "Brand" type = "xsd:string"/>
    <xsd:element name = "Model" type = "xsd:string"/>
    <xsd:element name = "Gear" type = "xsd:string"/>
    <xsd:element name = "Year" type = "xsd:int"/>
    <xsd:element name = "Information" type = "xsd:string"/>
    <xsd:complexType name = "CarType">
      <xsd:sequence>
          <xsd:element ref = "Brand"/>
          <xsd:element ref = "Model"/>
          <xsd:element ref = "Gear"/>
          <xsd:element ref = "Year"/>
          <xsd:element ref = "Information" minOccurs = "0" 
            maxOccurs = "1" /> 
      </xsd:sequence>
    </xsd:complexType>
    <xsd:element name = "Cars">
      <xsd:complexType>
          <xsd:sequence>
              <xsd:element name = "Car" type="CarType" 
                maxOccurs = "unbounded"/>
          </xsd:sequence>
      </xsd:complexType>
    </xsd:element>
    </xsd:schema>    
    

    XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <Cars xmlns:xs="http://www.w3.org/2001/XMLSchema" 
          xmlns="http://www.verkkokauppa.com">
     <Car>
      <Brand>BMW</Brand>
      <Model>535d</Model>
      <Gear>Automatic</Gear>
      <Year>2007</Year>
      <Information>It's a german.</Information>
     </Car>
    </Cars>
    

    【讨论】:

    • 您为什么认为这里存在命名空间问题? XML 的根元素是{http://www.verkkokauppa.com}Cars,并且模式清楚地声明了一个名为{http://www.verkkokauppa.com}Cars 的顶级元素。命名空间问题在哪里?
    【解决方案3】:

    这取决于您使用的 XML 模式验证器。可能是您在加载 XSD 文件时忘记包含指定 targetNamespace 的输入参数。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2013-12-27
    • 2012-07-02
    • 2012-06-07
    相关资源
    最近更新 更多