XML 文档不会验证 XSD

XML doc won't validate to XSD

我正在尝试学习 XML,并且我在网上找到了一个 XML 架构,我已经尝试制作一个实例文档。由于某种我不明白的原因,我的文档无法验证架构。有人可以指出我的错误吗?

Error message: Cause: Error on line 2: cvc-elt.1: Cannot find the declaration of element 'movies'.

文档:

<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>

以下更新的 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>

需要更新

  1. 添加了默认命名空间 (xmlns="http://movies.example") 以匹配 XSD.
  2. targetNamespace
  3. role/@ref 值更改为 "Leo" 以满足 cast-keyref key XSD.
  4. 的约束

可能值得注意的是 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>

( xsi:schemaLocation 可能 仍然需要以使 OP 的 XML 工具快乐)