XML Schema为多个子元素添加唯一ID

时间:2013-03-19 20:14:15

标签: xml validation schema unique

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

我设法使“主要”movieID成为唯一,而actorID在该movieID中是唯一的。但是,我还想让known_for/movies/movie元素中的movieID在其对应的actorID中是唯一的。我有点困惑在哪里需要为

定义唯一ID
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>

1 个答案:

答案 0 :(得分:1)

我通过向&#34; known_for&#34;添加唯一ID来解决问题。元素位于&#34; actorType&#34;

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