背景
使用架构验证XML文档。
问题
该问题的最简形式由两个文件显示。
XML 文档 file.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文档schema.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>
错误
来自xmllint的完整错误消息:
$ xmllint --noout --schema schema.xsd file.xml
file.xml:2: element recipe: Schemas validity error : Element 'recipe': No matching global declaration available for the validation root. file.xml fails to validate
问题
在给定的模式下,如何正确使用语法(或缺少哪些模式属性)才能确保可以成功验证给定的XML文档?
description
、title
等应该也有命名空间(例如:r:description
)。 - rbaleksandar