xml 没有可用于验证根的匹配全局声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8426154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No matching global declaration available for the validation root
提问by Dave Jarvis
Background
背景
Validate an XML document using a schema.
使用模式验证 XML 文档。
Problem
问题
The simplest form of the problem is shown in two files.
问题的最简单形式显示在两个文件中。
XML Document
XML 文件
<?xml version="1.0"?>
<recipe
xmlns:r="http://www.namespace.org/recipe">
<r:description>
<r:title>sugar cookies</r:title>
</r:description>
</recipe>
XSD Document
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>
Error
错误
The full error message from xmllint:
来自xmllint的完整错误消息:
file.xml:4: element recipe: Schemas validity error : Element 'recipe': No matching global declaration available for the validation root.
file.xml:4:元素配方:架构有效性错误:元素“配方”:没有可用于验证根的匹配全局声明。
Question
题
What is the correct syntax (or what schema attributes are missing) to ensure that the given schema can be used to successfully validate the given XML document?
确保给定模式可用于成功验证给定 XML 文档的正确语法(或缺少哪些模式属性)是什么?
采纳答案by tom redfern
You need to change your XML instance. Your current one says that there is a type called descriptionin the namespace http://www.namespace.org/recipe. However, in your XSD definition, the only types exposed in that namespace are called recipeand descriptionType.
您需要更改您的 XML 实例。您当前的一个说在名称空间http://www.namespace.org/recipe中有一种称为description的类型。但是,在您的 XSD 定义中,该命名空间中公开的唯一类型称为recipe和descriptionType。
So either define a type called descriptionin the XSD schema, or change your instance so you are referencing the recipetype correctly:
因此,要么在 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>
UPDATEThis is only half the solution - the other half is in @Aravind's answer here: https://stackoverflow.com/a/8426185/569662
更新这只是解决方案的一半 - 另一半在@Aravind 的回答中:https: //stackoverflow.com/a/8426185/569662
回答by Aravind Yarram
Only global element definitions can be used as root elements. Your schema only has complex types and hence the error. Change the <xsd:complexType name="recipe">to
只有全局元素定义可以用作根元素。您的架构只有复杂类型,因此会出现错误。更改 <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>
Read more about this here
在此处阅读更多相关信息
回答by Klesun
In my practice, I got the No matching global declaration available for the validation rootin two cases:
在我的实践中,我得到了No matching global declaration available for the validation root两种情况:
- If XSD does not contain an
<xsd:element name="recipe" .../>explained in @aravind-r-yarram's answer. If
<recipe/>in XML does not contain anxmlnsattribute. In such case adding thexmlnswill help:<recipe xmlns="http://www.namespace.org/recipe"> ... </recipe>
- 如果 XSD 不包含
<xsd:element name="recipe" .../>@aravind-r-yarram 的回答中的解释。 如果
<recipe/>在 XML 中不包含xmlns属性。在这种情况下,添加xmlns将有助于:<recipe xmlns="http://www.namespace.org/recipe"> ... </recipe>

