XML + Schema + 命名空间。验证根节点没有匹配的全局声明。

4

在使用命名空间时,引用模式的正确语法是什么?

问题

使用给定的模式创建XML文档。

错误


    .xml:9.20: Element '{http://example/buildings/1.0}old_buildings': No matching global declaration available for the validation root.
    oldbuildings.xml - invalid
    Problem

XML Document

<?xml version="1.0" encoding="UTF-8"?>

<buildings:old_buildings xmlns:buildings="http://example/buildings/1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://example/buildings/1.0 oldbuildings_schema.xsd">
    <building>
        <name>Name</name>
        <year_built era="BC">2000</year_built>
        <story>...<story>
    </building>
</buildings:old_buildings>

XSD文档

    <?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://example/buildings/1.0/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://example/buildings/1.0/"> <xs:element name="old_buildings"> <xs:complexType> <xs:sequence> <xs:element ref="building"/> </xs:sequence> </xs:complexType> </xs:element>
<xs:element name="building" type="buildingType"></xs:element>
<xs:complexType name="buildingType"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="year_built" type="yearType"/> <xs:element name="story" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="yearType"> <xs:simpleContent> <xs:extension base="xs:positiveInteger"> <xs:attribute name="era" type="xs:string"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:schema>
这个XSD文档定义了一个名为 "old_buildings" 的元素,它包含一个名为 "buildingType" 的复杂类型的元素。 "buildingType" 包含三个子元素:名称、年份和楼层数。年份是一个特殊类型 "yearType",有一个额外的属性 "era" 来指定时代。
2个回答

6
在您的XML文件中,请尝试使用以下内容:

xmlns:buildings="http://example/buildings/1.0/"

以斜杠结尾,就像您的XSD声明中一样:xs:schema targetNamespace="http://example/buildings/1.0/"

1
@BurhanAli 因为URI需要在模式和XML文件中匹配。其中一个URI缺少“/”。 - Fenil Dedhia

1

从xml文档中 缺少闭合标签

 <story>...<story>

To

<story>...</story>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接