2

我有一个存储电影及其演员的 XML 文件。有 2 个电影元素(+ 他们的 movieID 属性),每个元素包含 2 个 actorID。每个actorID 还包含known_for/movies/movie一个movieID。

我已经设法使“主要”movieID 是唯一的,并且 actorID 在该movieID 中是唯一的。但是,我还想让known_for/movies/movie元素内部的 movieID 在其相应的 actorID 中是唯一的。我有点困惑,我需要在哪里为

known_for/movies/movie  movieID 

它可能必须在里面的某个地方<xs:element name="movies,我只是不太确定到底在哪里。

这是我的 XML 文件

<?xml version="1.0" encoding="UTF-8"?>
<movies
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test.xsd">

<movie movieID="1">
    <title>Movie 1</title>
    <cast>
        <actors>
            <actor actorID="1"> 
                <name>Jack</name>
                <known_for>
                    <movies>
                        <movie movieID="1">Movie 1</movie>
                        <movie movieID="2">Movie 2</movie>
                        <movie movieID="3">Movie 3</movie>
                    </movies>
                </known_for>
            </actor>
            <actor actorID="2">
                <name>James</name>
                <known_for>
                    <movies>
                        <movie movieID="1">Movie 1</movie>
                        <movie movieID="2">Movie 2</movie>
                        <movie movieID="3">Movie 3</movie>
                    </movies>
                </known_for>
            </actor>
        </actors>   
    </cast>
</movie>

<movie movieID="2">
    <title>Movie 2</title>
    <cast>
        <actors>
            <actor actorID="1"> 
                <name>Jack</name>
                <known_for>
                    <movies>
                        <movie movieID="1">Movie 1</movie>
                        <movie movieID="2">Movie 2</movie>
                        <movie movieID="3">Movie 3</movie>
                    </movies>
                </known_for>
            </actor>
            <actor actorID="2">
                <name>James</name>
                <known_for>
                    <movies>
                        <movie movieID="1">Movie 1</movie>
                        <movie movieID="2">Movie 2</movie>
                        <movie movieID="3">Movie 3</movie>
                    </movies>
                </known_for>
            </actor>
        </actors>   
    </cast>
</movie>
</movies>

这是我的架构

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"  
    elementFormDefault="qualified" 
    attributeFormDefault="unqualified">

    <xs:element name="movies">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="movie" type="movieType" maxOccurs="unbounded">
                    <xs:unique name="unique_actorID">
                        <xs:selector xpath="cast/actors/actor"/>
                        <xs:field xpath="@actorID"/>
                    </xs:unique>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
        <xs:unique name="unique_movieID">
            <xs:selector xpath="movie"/>
            <xs:field xpath="@movieID"/>
        </xs:unique>
    </xs:element>

    <xs:complexType name="movieType">
        <xs:sequence>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="cast" type="castType"/>
        </xs:sequence>
        <xs:attribute name="movieID" type="xs:integer" use="required"/>
    </xs:complexType>

    <xs:complexType name="castType">
        <xs:sequence>
            <xs:element name="actors" type="actorsAll">
            </xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="actorsAll">
        <xs:sequence>
            <xs:element name="actor" type="actorType" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="actorType">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="known_for" type="known_forGroup" minOccurs="0"/>
        </xs:sequence>
        <xs:attribute name="actorID" type="xs:integer" use="required"/>
    </xs:complexType>

    <xs:complexType name="known_forGroup">
        <xs:sequence>
            <xs:element name="movies" type="known_forType"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="known_forType">
        <xs:sequence>
            <xs:element name="movie" type="known_forMovies" maxOccurs="unbounded">
            </xs:element>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="known_forMovies">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="movieID" type="xs:integer"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

</xs:schema>
4

1 回答 1

2

我已通过向位于“actorType”内的“known_for”元素添加唯一 ID 来解决此问题

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"  
    elementFormDefault="qualified" 
    attributeFormDefault="unqualified">

    <xs:element name="movies">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="movie" type="movieType" maxOccurs="unbounded">
                    <xs:unique name="unique_actorID">
                    <xs:selector xpath="cast/actors/actor"/>
                    <xs:field xpath="@actorID"/>
                </xs:unique>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:unique name="unique_movieID">
        <xs:selector xpath="movie"/>
        <xs:field xpath="@movieID"/>
    </xs:unique>
</xs:element>

<xs:complexType name="movieType">
    <xs:sequence>
        <xs:element name="title" type="xs:string"/>
        <xs:element name="cast" type="castType"/>
    </xs:sequence>
    <xs:attribute name="movieID" type="xs:integer" use="required"/>
</xs:complexType>

<xs:complexType name="castType">
    <xs:sequence>
        <xs:element name="actors" type="actorsAll">
        </xs:element>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="actorsAll">
    <xs:sequence>
        <xs:element name="actor" type="actorType" maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="actorType">
    <xs:sequence>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="known_for" type="known_forGroup" minOccurs="0">
            <xs:unique name="unique_known_for_movieID">
                <xs:selector xpath="movies/movie"/>
                <xs:field xpath="@movieID"/>
            </xs:unique>
        </xs:element>
    </xs:sequence>
    <xs:attribute name="actorID" type="xs:integer" use="required"/>
</xs:complexType>

<xs:complexType name="known_forGroup">
    <xs:sequence>
        <xs:element name="movies" type="known_forType"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="known_forType">
    <xs:sequence>
        <xs:element name="movie" type="known_forMovies" maxOccurs="unbounded">
        </xs:element>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="known_forMovies">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute name="movieID" type="xs:integer"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

</xs:schema>
于 2013-03-23T20:27:09.807 回答