本文介绍了XML Schema 为多个子元素添加唯一 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an XML file that stores movies and their actors. There are 2 movie elements (+ their movieID attribute) that contain 2 actorID each. Each actorID also contains known_for/movies/movie that also has a movieID.

我已经设法使主要"movieID 在该movieID 中唯一,actorID 也唯一.但是,我还想让 known_for/movies/movie 元素中的 movieID 在其相应的 actorID 中是唯一的.我对在哪里需要为

I have managed to make "the main" movieID to be unique and actorID to be unique within that movieID. However, I also want to make movieID inside known_for/movies/movie element to be unique within their corresponding actorID. I am a bit confused on where do I need to define unique id for the

known_for/movies/movie  movieID 

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

It probably has to go somewhere inside <xs:element name="movies, I'm just not too sure where exactly.

这是我的 XML 文件

This is my XML file

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

推荐答案

我已通过向位于actorType"内的known_for"元素添加唯一 id 来解决该问题

I have fixed the problem by adding unique id to the "known_for" element located inside the "actorType"

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

这篇关于XML Schema 为多个子元素添加唯一 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 07:17