What would be a good schema for this xml file

Discussion in 'XML' started by Mark Constant, Nov 29, 2003.

  1. 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>
    Mark Constant, Nov 29, 2003
    #1
    1. Advertising

  2. (Mark Constant) writes:

    > 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
    C. M. Sperberg-McQueen, Nov 30, 2003
    #2
    1. Advertising

  3. (C. M. Sperberg-McQueen) wrote in message news:<>...
    > (Mark Constant) writes:
    >
    > > 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.
    Mark Constant, Dec 1, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,497
    Markus
    Nov 23, 2005
  2. Fred Smith
    Replies:
    1
    Views:
    928
    Martin Honnen
    Feb 5, 2004
  3. Hari Om
    Replies:
    1
    Views:
    451
    Martin SChukrazy
    Mar 2, 2004
  4. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,173
    Stanimir Stamenkov
    Apr 25, 2005
  5. Replies:
    3
    Views:
    2,797
Loading...

Share This Page