How to improve XML schema with sub-elements or references?

141 views Asked by At

I have a xml schema like the one being shown below but I have noticed that it would be very complicated to maintain.

I want to improve the current schema in one of the following ways but I am not sure if they would be the best/correct route:

Option 1: Use a reference of all elements of movieDetails within movies.

Option 2: Add movieDetails as a sub-element of movies (which I frankly am struggling to achieve)

Which route would be best and what would be the best way to structure it?

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                    targetNamespace="http://xml.netbeans.org/schema/AmazingMovies"
                    xmlns:tns="http://xml.netbeans.org/schema/AmazingMovies"
                    elementFormDefault="qualified">

    <xsd:complexType name="movies">
        <xsd:sequence>
            <xsd:element name="Title" type="xsd:string"/>
            <xsd:element name="Director" type="xsd:string"></xsd:element>
            <xsd:element name="Year" type="xsd:int"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="movieDetails">
        <xsd:sequence>
            <xsd:element name="Actor" type="xsd:string"/>
            <xsd:element name="numberOfCast" type="xsd:int"></xsd:element>
            <xsd:element name="releaseDate" type="xsd:date"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:element name="OnDisplay">
        <xsd:complexType>
            <xsd:sequence >
                <xsd:element name="collectionOfMovies" type="tns:movies" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

</xsd:schema>

Extra information

OnDisplay element would add all elements of movies into a list in order to use them when unMarshalling. However, I want to also add movieDetails onto OnDisplay, therefore, it would be convenient to have them included in movies as a ref, sub-element or another form.

Tried the following methods but failed XML validation

Method 1

<xsd:element name="MoviesAndDetails">
    <xsd:complexType name="movies">
        <xsd:sequence>
            <xsd:element name="Title" type="xsd:string"/>
            <xsd:element name="Director" type="xsd:string"></xsd:element>
            <xsd:element name="Year" type="xsd:int"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="movieDetails">
        <xsd:sequence>
            <xsd:element name="Actor" type="xsd:string"/>
            <xsd:element name="numberOfCast" type="xsd:int">     </xsd:element>
            <xsd:element name="releaseDate" type="xsd:date"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

Method 2

<xsd:complexType name="MoviesAndDetails">
    <xsd:complexType name="movies">
        <xsd:sequence>
            <xsd:element name="Title" type="xsd:string"/>
            <xsd:element name="Director" type="xsd:string"></xsd:element>
            <xsd:element name="Year" type="xsd:int"/>
            <xsd:element ref="Actor"/>
            <xsd:element ref="numberOfCast"/>
            <xsd:element ref="releaseDate"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="movieDetails">
        <xsd:sequence>
            <xsd:element name="Actor" type="xsd:string"/>
            <xsd:element name="numberOfCast" type="xsd:int">     </xsd:element>
            <xsd:element name="releaseDate" type="xsd:date"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>
1

There are 1 answers

2
Mads Hansen On

If you want to have a movieDetails element as a child of collectionOfMovies, then you could add a movieDetails element of type tns:movieDetails to the sequence of movies elements.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://xml.netbeans.org/schema/AmazingMovies"
  xmlns:tns="http://xml.netbeans.org/schema/AmazingMovies"
  elementFormDefault="qualified">

  <xsd:complexType name="movies">
    <xsd:sequence>
        <xsd:element name="Title" type="xsd:string"/>
        <xsd:element name="Director" type="xsd:string"></xsd:element>
        <xsd:element name="Year" type="xsd:int"/>
        <xsd:element name="movieDetails" type="tns:movieDetails"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:complexType name="movieDetails">
    <xsd:sequence>
        <xsd:element name="Actor" type="xsd:string"/>
        <xsd:element name="numberOfCast" type="xsd:int"></xsd:element>
        <xsd:element name="releaseDate" type="xsd:date"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="OnDisplay">
    <xsd:complexType>
        <xsd:sequence >
            <xsd:element name="collectionOfMovies" type="tns:movies" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

</xsd:schema>