15
votes

I'm working on my xml skills, but validator errors my XSD at certain deep rooted places. The erorrs are:

  • 18: 11 cvc-complex-type.2.4.d: Invalid content was found starting with element 'aktor'. No child element is expected at this point.
  • 37: 11 cvc-complex-type.2.4.d: Invalid content was found starting
    with element 'utwor'. No child element is expected at this point.

Here's XSD:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="filmy">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="film">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="tytul"/>
                        <xs:element name="gatunek"/>
                        <xs:element name="czasTrwania"/>
                        <xs:element name="premiera"/>
                        <xs:element name="produkcja"/>
                        <xs:element name="rezyser"/>
                        <xs:element name="scenariusz"/>

                        <xs:element name="obsada">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="aktor">
                                        <xs:complexType>
                                            <xs:sequence>
                                                <xs:element name="imie"/>
                                                <xs:element name="nazwisko"/>
                                                <xs:element name="dataUrodzenia"/>
                                                <xs:element name="postac"/>
                                            </xs:sequence>
                                        </xs:complexType>
                                    </xs:element>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>

                        <xs:element name="soundtrack">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="utwor">
                                        <xs:complexType>
                                            <xs:sequence>
                                                <xs:element name="wykonawca"/>
                                                <xs:element name="tytulUtworu"/>
                                                <xs:element name="gatunekMuzyczny"/>
                                            </xs:sequence>
                                        </xs:complexType>
                                    </xs:element>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

</xs:schema>

Here's XML:

<?xml version="1.0" encoding="utf-8"?>
<filmy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <film>
        <tytul>Fight Club</tytul>
        <gatunek>Thriller, Psychologiczny</gatunek>
        <czasTrwania>2h11m</czasTrwania>
        <premiera>11 luty 1999</premiera>
        <produkcja>Niemcy, USA</produkcja>
        <rezyser>David Fincher</rezyser>
        <scenariusz>Jim Uhls</scenariusz>
        <obsada>
            <aktor>
                <imie>Edward</imie>
                <nazwisko>Norton</nazwisko>
                <dataUrodzenia>1969</dataUrodzenia>
                <postac>Narrator</postac>
            </aktor>
            <aktor>
                <imie>Brad</imie>
                <nazwisko>Pitt</nazwisko>
                <dataUrodzenia>1963</dataUrodzenia>
                <postac>Tyler Durden</postac>
            </aktor>
            <aktor>
                <imie>Helena</imie>
                <nazwisko>Boham Carter</nazwisko>
                <dataUrodzenia>1966</dataUrodzenia>
                <postac>Marla Singer</postac>
            </aktor>
        </obsada>
        <soundtrack>
            <utwor>
                <wykonawca>The Pixies</wykonawca>
                <tytulUtworu>Where is my mind</tytulUtworu>
                <gatunekMuzyczny>Rock</gatunekMuzyczny>
            </utwor>
            <utwor>
                <wykonawca>The Pixies</wykonawca>
                <tytulUtworu>Where is my mind</tytulUtworu>
                <gatunekMuzyczny>Rock</gatunekMuzyczny>
            </utwor>
            <utwor>
                <wykonawca>The Pixies</wykonawca>
                <tytulUtworu>Where is my mind</tytulUtworu>
                <gatunekMuzyczny>Rock</gatunekMuzyczny>
            </utwor>
        </soundtrack>
    </film>
</filmy>

I don't understand the problem, it says no child element is expected, is there a limit for child number? Can i expand it?

I've found answer to my question, elements that could be multiplied, has to have maxOccurs="unbounded" attribute.

3
You should post the solution as an answer, then wait 48 hours and "accept" your own answer. This will mark the question answered, with the correct solution, for future users. You won't get any points, but you will be making the site betterJim Garrison
Actually you will get 2 points for marking an answer.tom redfern

3 Answers

18
votes

I've found answer to my question, elements that could be multiplied, has to have maxOccurs="unbounded" attribute.

3
votes

The answer accepted for this question, will indeed solve the problem, but is not the "right" solution.

The reason, adding unbounded, works, is that the xsd now allow multiple sequences, i.e.

  <filmy>
    <sequence>
      <element_3>
    </sequence>  
    <sequence>
      <element_2>
    <sequence>
    <sequence>
      <element_1>
    <sequence>
  </filmy>

What you SHOULD do to solve the problem, is add the elements of the sequence, to the content, in the correct order, i.e. something like this:

  filmy.getContent().add(element1)
  filmy.getContent().add(element2)
  filmy.getContent().add(element3)

which will give you:

  <filmy>
    <sequence>
      <element_1>
      <element_2>
      <element_3>
    <sequence>
  </filmy>

i.e. only ONE sequence, and thus no need for unbounded.

2
votes

Just adding another answer here as the accepted one didn't completely help me.

So if you have a structure like the question asker here, you can't put the maxOccurs="unbounded" on the outer most element wrapper as it will be considered a global element.

You have to instead put it on all the potential elements like so:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="filmy">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="film">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element maxOccurs="unbounded" name="tytul"/>
                        <xs:element maxOccurs="unbounded" name="gatunek"/>
                        <xs:element maxOccurs="unbounded" name="czasTrwania"/>
                        <xs:element maxOccurs="unbounded" name="premiera"/>
                        <xs:element maxOccurs="unbounded" name="produkcja"/>
                        <xs:element maxOccurs="unbounded" name="rezyser"/>
                        <xs:element maxOccurs="unbounded" name="scenariusz"/>

...etc