Search code examples
xmldtddtd-parsing

XML and DTD: The content of element type must match


I'm learning XML at the moment and I'm struggling with the first DTD extension.

My DTD :

<!ELEMENT  biblio (livre*) >

<!ELEMENT  livre (achat , auteurs, titre ) >
<!ATTLIST livre langue CDATA  #IMPLIED
         ref  CDATA  #IMPLIED>



<!ELEMENT  achat EMPTY >
<!ATTLIST achat  date CDATA #IMPLIED
        lieu CDATA #IMPLIED>


<!ELEMENT  titre (#PCDATA)>
<!ATTLIST titre genre CDATA #IMPLIED
        type NMTOKEN #IMPLIED>



<!ELEMENT  auteurs (auteur+) >


<!ELEMENT  auteur ( nom?, prenom? ,sexe?) >
<!ELEMENT nom (#PCDATA)>
<!ELEMENT prenom (#PCDATA)>
<!ELEMENT sexe (#PCDATA)>

If I launched the parser,it seems :

The content of element type "livre" must match (achat , auteus ,titre )

My XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE biblio SYSTEM  "Dtdbiblio.dtd">



<biblio>
    <livre langue="francais" ref="1684561564">
        <achat date="11/11/1993" lieu="london"/>
        <titre genre="G" type="politique">      Tiiiiiiiiiiiiiiiiiiiiiiiiitre </titre>
        <auteurs>
            <auteur>
                <nom>x</nom>
                <prenom>x</prenom>
                <sexe>H</sexe>
            </auteur>
        </auteurs>
    </livre>

</biblio>

How resolve this problem?


Solution

  • The commas (,) in (achat , auteurs, titre ) specify the order that the elements must appear. (See here for more detail.)

    So (achat , auteurs, titre ) means exactly one achat followed by exactly one auteurs followed by exaclty one titre.

    You just need to change the order of titre and auteurs... either in the XML instance:

    <biblio>
        <livre langue="francais" ref="1684561564">
            <achat date="11/11/1993" lieu="london"/>
            <auteurs>
                <auteur>
                    <nom>x</nom>
                    <prenom>x</prenom>
                    <sexe>H</sexe>
                </auteur>
            </auteurs>
            <titre genre="G" type="politique">      Tiiiiiiiiiiiiiiiiiiiiiiiiitre </titre>
        </livre>
    
    </biblio>
    

    or in the DTD:

    <!ELEMENT  livre (achat, titre, auteurs) >