I've got a problem with configuring XML + XSD. I wanted to use a namespace in my XML, but I don't know how to write a XSD for this.
我在配置XML + XSD时遇到了问题。我想在我的XML中使用命名空间,但我不知道如何为此编写XSD。
The 'a' namespace stands for 'actor'. I know it isn't good idea to do something like that, but it's just a excercise :)
'a'命名空间代表'actor'。我知道做这样的事情不是一个好主意,但它只是一个练习:)
If I would use something like
如果我会使用类似的东西
<role name="xxx">
<actorName>asd</actorName>
...
</role>
etc. it will be valid, but I really wanted to learn how to use namespaces.
它将是有效的,但我真的想学习如何使用命名空间。
This is my XML file:
这是我的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 im*ed 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>
And my XSD:
而我的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>
Can you help me do it properly?
你能帮我做好吗?
1 个解决方案
#1
0
try this: role.xsd:
试试这个: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 im*ed 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>
#1
0
try this: role.xsd:
试试这个: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 im*ed 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>