XML:如何创建有效的namespace + xsd架构

时间:2015-05-02 10:42:19

标签: xml xsd namespaces

我遇到配置XML + XSD的问题。我想在我的XML中使用命名空间,但我不知道如何为此编写XSD。

'a'命名空间代表'actor'。我知道做这样的事情不是一个好主意,但它只是一个练习:)

如果我会使用像

这样的东西
<role name="xxx">
    <actorName>asd</actorName>
    ...
</role>

等。它会有效,但我真的想学习如何使用命名空间。

这是我的XML文件:

<?xml version="1.0"?>
<catalog xmlns:a="actor">
    <movie>
        <title>The Shawshank Redemption</title>
        <year>1994</year>
        <director>Frank Darabont</director>
        <screenplay>Frank Darabont</screenplay>
        <genre>Drama</genre>
        <country>USA</country>
        <description>Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.</description>
        <stars>
            <role name="Andy Dufresne">
                <a:name>Tim Robbins</a:name>
                <a:year>1958</a:year>
                <a:country>USA</a:country>
            </role>
            <role name="Ellis Boyd 'Red' Redding">
                <a:name>Morgan Freeman</a:name>
                <a:year>1937</a:year>
                <a:country>USA</a:country>
            </role>
            <role name="Warden Norton">
                <a:name>Bob Gunton</a:name>
                <a:year>1945</a:year>
                <a:country>USA</a:country>
            </role>
        </stars>
    </movie>
    <movie>
        <title>The Godfather</title>
        <year>1972</year>
        <director>Francis Ford Coppola</director>
        <screenplay>Mario Puzo, Francis Ford Coppola</screenplay>
        <genre>Crime, Drama</genre>
        <country>USA</country>
        <description>The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.</description>
        <stars>
            <role name="Don Vito Corleone">
                <a:name>Marlon Brando</a:name>
                <a:year>1924</a:year>
                <a:country>USA</a:country>
            </role>
            <role name="Michael Corleone">
                <a:name>Al Pacino</a:name>
                <a:year>1940</a:year>
                <a:country>USA</a:country>
            </role>
            <role name="Sonny Corleone">
                <a:name>James Caan</a:name>
                <a:year>1940</a:year>
                <a:country>USA</a:country>
            </role>
        </stars>
    </movie>
</catalog>

我的XSD:

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="urn:a" >
   <xs:element name="catalog">
   <xs:complexType>
   <xs:sequence>
       <xs:element name="movie" maxOccurs="unbounded">
       <xs:complexType>
       <xs:sequence>
           <xs:element name="title" type="xs:string"/>
           <xs:element name="year" type="xs:short"/>
           <xs:element name="director" type="xs:string"/>
           <xs:element name="screenplay" type="xs:string"/>
           <xs:element name="genre" type="xs:string"/>
           <xs:element name="country" type="xs:string"/>
           <xs:element name="description">
               <xs:simpleType>
               <xs:restriction base="xs:string">
                   <xs:whiteSpace value="collapse"/>
               </xs:restriction>
               </xs:simpleType>
           </xs:element>
           <xs:element name="stars">
            <xs:complexType>
            <xs:sequence>
                <xs:element name="role" maxOccurs="unbounded">
                    <xs:complexType>
                    <xs:sequence>
                        <xs:element name="name" type="xs:string"/>
                        <xs:element name="year" type="xs:short"/>
                        <xs:element name="country" type="xs:string"/>
                    </xs:sequence>
                    <xs:attribute name="name" type="xs:string"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            </xs:complexType>
           </xs:element>
       </xs:sequence>
       </xs:complexType>
       </xs:element>
   </xs:sequence>
   </xs:complexType>
   </xs:element>
</xs:schema>

你能帮我正确地做吗?

0 个答案:

没有答案