我正在按照一本基础书学习XML。完全按照书上的内容,稍微编辑了一下XML,就给了我这个错误。
cvc-elt.1: Cannot find the declaration of element 'games'.
XML:
<?xml version="1.0" encoding="utf-8"?>
<games
xmlns="http://tempuri.org/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tempuri.org game.xsd">
<game>
<title>Red Dead Redeption Two</title>
<platform>Xbox One</platform>
<desc>Red Dead Redeption Two is a western action adventure game</desc>
<img>Cover.png</img>
</game>
</games>
XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
targetNamespace="http://tempuri.org"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema games.xsd">
<xs:element name="games">
<xs:complexType>
<xs:sequence>
<xs:element name="game" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="platform" type="xs:string"/>
<xs:element name="desc" type="xs:string"/>
<xs:element name="img" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
变化
xmlns:xs="http://www.w3.org/2001/XMLSchema games.xsd"
到
xmlns:xs="http://www.w3.org/2001/XMLSchema"
因为后者是 XMLSchema 命名空间的实际定义,而前者似乎是一个错误的尝试,它遵循了 XMLSchema 命名空间的形式。schemaLocation
属性。
删除以下内容。
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
因为XSD的默认命名空间不应该是 http://tempuri.org/XMLSchema.xsd
并且由于 mstns
命名空间前缀声明是不需要的。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
targetNamespace="http://tempuri.org"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="games">
<xs:complexType>
<xs:sequence>
<xs:element name="game" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="platform" type="xs:string"/>
<xs:element name="desc" type="xs:string"/>
<xs:element name="img" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
变化
xmlns="http://tempuri.org/"
将要
xmlns="http://tempuri.org"
以配合双方的 targetNamespace
的XSD和 schemaLocation
在XML中。
<?xml version="1.0" encoding="utf-8"?>
<games
xmlns="http://tempuri.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tempuri.org games.xsd">
<game>
<title>Red Dead Redeption Two</title>
<platform>Xbox One</platform>
<desc>Red Dead Redeption Two is a western action adventure game</desc>
<img>Cover.png</img>
</game>
</games>
在进行上述修改后,您的XML将成功地与您的XSD进行验证。