What would be a good schema for this xml file

M

Mark Constant

All I want to do is have a xml file like this
<Entertainment>
<Movie>
<Title>(Title Here)</Title>
<Rating>(Rating Here)</Title>
</Movie>

<Movie>
<Title>(Title Here)</Title>
<Rating>(Rating Here)</Title>
</Movie>

<Movie>
<Title>(Title Here)</Title>
<Rating>(Rating Here)</Title>
</Movie>

<PS2>
<Title>(Title Here)</Title>
<Rating>(Rating Here)</Title>
</PS2>
</Entertainment>

I try something like the schema below but this schema doesn't work.
With this schema it is looking for a xml file like this. I want to be
able to add as many Movie or PS2 elements in any order without errors.

<Entertainment>
<Movie>
<Title>(Title Here)</Title>
<Rating>(Rating Here)</Title>
</Movie>


<PS2>
<Title>(Title Here)</Title>
<Rating>(Rating Here)</Title>
</PS2>
</Entertainment>


<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="Entertainment" targetNamespace="http://mark/Homepage/"
elementFormDefault="qualified" xmlns="http://mark/Homepage/"
xmlns:mstns="http://mark/Homepage/"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Entertainment">
<xs:complexType>
<xs:sequence>
<xs:element name="Movie" type="Information"></xs:element>
<xs:element name="PS2" type="Information" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Information">
<xs:sequence>
<xs:element name="Description" type="xs:string" />
<xs:element name="Genre" type="xs:string" />
<xs:element name="Hardware" type="xs:string" />
<xs:element name="Picture" type="xs:string" />
<xs:element name="Rating" type="xs:positiveInteger" />
<xs:element name="Title" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
 
C

C. M. Sperberg-McQueen

All I want to do is have a xml file like this
[example elided]

I try something like the schema below but this schema doesn't work.
With this schema it is looking for a xml file like this. I want to be
able to add as many Movie or PS2 elements in any order without errors.

[example with single Movie and single PS2 element]

...
<xs:element name="Entertainment">
<xs:complexType>
<xs:sequence>
<xs:element name="Movie" type="Information"></xs:element>
<xs:element name="PS2" type="Information" />
</xs:sequence>
</xs:complexType>
</xs:element>

Here's your problem: you are telling the schema processor that what
you want in an Entertainment element is a sequence consisting of
exactly one Movie element and exactly one PS2 element, in order.

You can use minOccurs and maxOccurs attributes on the xs:sequence or
xs:element elements to specify that more than one element can occur.

If we just specify maxOccurs="unbounded" on the two elements, we allow
any positive number of Movie elements, followed by any positive number
of PS2 elements:

<xs:complexType>
<xs:sequence>
<xs:element name="Movie" type="Information" maxOccurs="unbounded"/>
<xs:element name="PS2" type="Information" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

If you want to allow Entertainment elements without Movie elements
or PS2 elements, you can also add minOccurs="0" to the xs:element
elements.

Since you want to allow the Movie and PS2 elements to occur in any
order, however, you really need to change something else, too. One
way is to allow the sequence to repeat:

<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="Movie" type="Information"
minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="PS2" type="Information"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

Note that since the sequence can repeat, the maxOccurs attributes on
the xs:element elements are no longer necessary and can be removed.

It is probably more usual, however, to express 'any number of Movie or
PS2 elements, in any order' not as a repeating sequence but as a
repeating choice, thus:

<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Movie" type="Information"/>
<xs:element name="PS2" type="Information"/>
</xs:choice>
</xs:complexType>

I hope this helps.

-C. M. Sperberg-McQueen
World Wide Web Consortium
 
M

Mark Constant

All I want to do is have a xml file like this
[example elided]

I try something like the schema below but this schema doesn't work.
With this schema it is looking for a xml file like this. I want to be
able to add as many Movie or PS2 elements in any order without errors.

[example with single Movie and single PS2 element]

...
<xs:element name="Entertainment">
<xs:complexType>
<xs:sequence>
<xs:element name="Movie" type="Information"></xs:element>
<xs:element name="PS2" type="Information" />
</xs:sequence>
</xs:complexType>
</xs:element>

Here's your problem: you are telling the schema processor that what
you want in an Entertainment element is a sequence consisting of
exactly one Movie element and exactly one PS2 element, in order.

You can use minOccurs and maxOccurs attributes on the xs:sequence or
xs:element elements to specify that more than one element can occur.

If we just specify maxOccurs="unbounded" on the two elements, we allow
any positive number of Movie elements, followed by any positive number
of PS2 elements:

<xs:complexType>
<xs:sequence>
<xs:element name="Movie" type="Information" maxOccurs="unbounded"/>
<xs:element name="PS2" type="Information" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

If you want to allow Entertainment elements without Movie elements
or PS2 elements, you can also add minOccurs="0" to the xs:element
elements.

Since you want to allow the Movie and PS2 elements to occur in any
order, however, you really need to change something else, too. One
way is to allow the sequence to repeat:

<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="Movie" type="Information"
minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="PS2" type="Information"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

Note that since the sequence can repeat, the maxOccurs attributes on
the xs:element elements are no longer necessary and can be removed.

It is probably more usual, however, to express 'any number of Movie or
PS2 elements, in any order' not as a repeating sequence but as a
repeating choice, thus:

<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Movie" type="Information"/>
<xs:element name="PS2" type="Information"/>
</xs:choice>
</xs:complexType>

I hope this helps.

-C. M. Sperberg-McQueen
World Wide Web Consortium

That helped a lot thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top