我正在尝试学习XML,并且我在线找到了一个XML Schema,我尝试制作一个实例文档。由于某些我无法获得的原因,我的文档无法验证模式。有人可以指出我的错误吗?
错误消息:原因:第2行出错:cvc-elt.1:找不到 元素宣言'电影'。
文档:
<movies>
<movie>
<title>The Revenant</title>
<length>120</length>
<year>2015</year>
<cast>
<role ref="Lead">
</role>
</cast>
</movie>
<person id="Leo">
<name>Leonardo DiCaprio</name>
<birth>1982-12-13</birth>
</person>
</movies>
模式:
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:m="http://movies.example"
targetNamespace="http://movies.example"
elementFormDefault="qualified">
<element name="movies">
<complexType>
<sequence>
<element ref="m:movie" minOccurs="0" maxOccurs="unbounded"/>
<element ref="m:person" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
<unique name="movies-unique">
<selector xpath="m:movie"/>
<field xpath="m:title"/>
<field xpath="m:year"/>
</unique>
<key name="cast-key">
<selector xpath="m:person|m:star"/>
<field xpath="@id"/>
</key>
<keyref refer="m:cast-key" name="cast-keyref">
<selector xpath=".//m:role"/>
<field xpath="@ref"/>
</keyref>
</element>
<element name="movie">
<complexType>
<sequence>
<element name="title" type="string"/>
<element name="length" type="nonNegativeInteger"/>
<element name="year" type="gYear"/>
<element name="cast">
<complexType>
<sequence maxOccurs="unbounded">
<element name="role">
<complexType mixed="true">
<attribute name="ref" type="NCName"/>
</complexType>
</element>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
<element name="person" type="m:personType"/>
<complexType name="personType">
<sequence>
<element name="name" type="string"/>
<element name="birth" type="date"/>
</sequence>
<attribute name="id" type="NCName"/>
</complexType>
<element name="star" substitutionGroup="m:person">
<complexType>
<complexContent>
<extension base="m:personType">
<sequence maxOccurs="unbounded">
<element name="award" type="string"/>
</sequence>
</extension>
</complexContent>
</complexType>
</element>
</schema>
答案 0 :(得分:2)
以下更新的XML将针对您的XSD成功验证:
<movies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://movies.example"
xsi:schemaLocation="http://movies.example try.xsd">
<movie>
<title>The Revenant</title>
<length>120</length>
<year>2015</year>
<cast>
<role ref="Leo"/>
</cast>
</movie>
<person id="Leo">
<name>Leonardo DiCaprio</name>
<birth>1982-12-13</birth>
</person>
</movies>
需要更新:
xmlns="http://movies.example"
)
XSD的targetNamespace
。role/@ref
值更改为"Leo"
以满足cast-keyref
XSD的key
约束。答案 1 :(得分:-1)
值得注意的是,通常不需要xsi:schemaLocation属性,尽管XMLSpy和其他工具经常插入它。唯一的必需更改是添加默认命名空间声明:
<movies xmlns="http://movies.example">
<movie>
<title>The Revenant</title>
<length>120</length>
<year>2015</year>
<cast>
<role ref="Leo"/>
</cast>
</movie>
<person id="Leo">
<name>Leonardo DiCaprio</name>
<birth>1982-12-13</birth>
</person>
</movies>
(为了使OP的XML工具满意,仍然需要xsi:schemaLocation )