2015-05-02 3 views
0

У меня возникла проблема с настройкой XML + XSD. Я хотел использовать пространство имен в своем XML, но я не знаю, как написать XSD для этого.XML: Как создать допустимое пространство имен + схема xsd

Пространство имен 'a' означает «актер». Я знаю, что это не хорошая идея, чтобы сделать что-то подобное, но это просто Excercise :)

Если я хотел бы использовать что-то вроде

<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

попробовать это: role.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="actor"> 
    <xs:element name="name" type="xs:string" /> 
    <xs:element name="year" type="xs:short"/> 
    <xs:element name="country" type="xs:string"/> 
</xs:schema> 

catalog.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:a = 'actor'> 
    <xs:import namespace="actor" schemaLocation="role.xsd"/> 
    <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 ref="a:name"/> 
                <xs:element ref="a:year"/> 
                <xs:element ref="a:country"/> 
               </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> 

catalog.xml:

<catalog xmlns:a="actor" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="catalog.xsd"> 
    <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>