验证根目录没有可用的匹配全局声明

Dav*_*vis 49 xml xsd xmllint

背景

使用模式验证XML文档.

问题

最简单的问题形式显示在两个文件中.

XML文档

<?xml version="1.0"?>

<recipe
  xmlns:r="http://www.namespace.org/recipe">

<r:description>
  <r:title>sugar cookies</r:title>
</r:description>

</recipe>
Run Code Online (Sandbox Code Playgroud)

XSD文件

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema
   version="1.0"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:r="http://www.namespace.org/recipe">

  <xsd:complexType name="recipe">
    <xsd:choice>
      <xsd:element name="description" type="descriptionType"
        minOccurs="1" maxOccurs="1" />
    </xsd:choice>
  </xsd:complexType>

  <xsd:complexType name="descriptionType">
    <xsd:all>
      <xsd:element name="title">
        <xsd:simpleType>
          <xsd:restriction base="xsd:string">
            <xsd:minLength value="5" />
            <xsd:maxLength value="55" />
          </xsd:restriction>
        </xsd:simpleType>
      </xsd:element>
    </xsd:all>
  </xsd:complexType>
</xsd:schema>
Run Code Online (Sandbox Code Playgroud)

错误

来自xmllint的完整错误消息:

file.xml:4:元素配方:模式有效性错误:元素'配方':没有可用于验证根的匹配全局声明.

确保给定模式可用于成功验证给定XML文档的正确语法(或缺少哪些模式属性)是什么?

tom*_*ern 28

您需要更改XML实例.您当前的人说它正在寻找名称空间http://www.namespace.org/recipe中的一种称为描述的类型.但是,该命名空间中公开的唯一类型称为recipedescriptionType.

因此,要么在XSD架构中定义名为description的类型,要么更改实例,以便正确引用配方类型:

<?xml version="1.0" encoding="utf-8"?>
<r:recipe
  xmlns:r="http://www.namespace.org/recipe">
  <description>
    <title>sugar cookies</title>
  </description>
</r:recipe>
Run Code Online (Sandbox Code Playgroud)


Ara*_*ram 14

只有全局元素定义可以用作根元素.您的架构只有复杂的类型,因此错误.更改 <xsd:complexType name="recipe">

<xsd:element name="recipe">
  <xsd:complexType>
    <xsd:choice>
      <xsd:element name="description" type="descriptionType"
        minOccurs="1" maxOccurs="1" />
    </xsd:choice>
  </xsd:complexType>
</xsd:element>
Run Code Online (Sandbox Code Playgroud)

在这里阅读更多相关信息

  • 将elementFormDefault ="qualified"添加到xs:schema元素中.这意味着假定模式中的本地声明的元素位于目标命名空间中.这应该是默认值,您应该将其视为样板 - 相反的情况,其中本地元素没有名称空间,是非常罕见的. (8认同)

Art*_*sun 8

在我的实践中,我得到了No matching global declaration available for the validation root两种情况: