Search code examples
xmldtd

why do i have this error element type "message" must not be declared more than once


I have this dtd file

<!DOCTYPE messages
[
<!ELEMENT messages (message,contact,group)>
<!ELEMENT message (transmitter,receiver)>
<!ELEMENT message (#PCDATA)>
<!ELEMENT transmitter (#PCDATA)>
<!ATTLIST message type (text|vocal|file) >

<!ELEMENT receiver (#PCDATA)>
<!ELEMENT receiver(contact,group)>
<!ELEMENT contact (contact_name,contact_first_name )>
<!ELEMENT contact_name (#PCDATA)>
<!ELEMENT contact_first_name (#PCDATA)>
<!ELEMENT group(name_group,file_group)>
<!ELEMENT name_group (#PCDATA)>
<!ELEMENT file_group (#PCDATA)>
<!ATTLIST file_group type(text | vocal| file)>
]>

and this is my xml file my goal is to assign this file to an xml file

<messages>
    <message type="text">
        <transmitter> Brahim </transmitter>
        <receiver> 
            <contact>
            
            </contact>
            <group>
            
            </group>
         </receiver>
    </message>
    <contact>
        <contact_name> Brahim Elmoctar </contact_name>
        <contact_first_name> TLEIMIDI </contact_first_name>
    </contact>
    <group>
    <name_group> M1SRT</name_group>
    <file_group type="file"> </file_group>
    </group>
</messages>

the xml work fine but when i add the dtd file it's stop working. can you explain to me why i have this error ?


Solution

  • You can't declare an element more than once. For example, you have "message" declared twice:

    <!ELEMENT message (transmitter,receiver)>
    <!ELEMENT message (#PCDATA)>
    

    If you want message to allow both child elements and PCDATA (referred to as "mixed content") you must declare it a certain way:

    <!ELEMENT message (#PCDATA|transmitter|receiver)*>
    

    You'd need to do the same thing with receiver.

    If you don't need mixed content, simply remove the "PCDATA" declaration.

    There are a few other issues as well...

    • You have to have a space between the element name and model. For example <!ELEMENT group(name_group,file_group)> should be <!ELEMENT group (name_group,file_group)> (note the space after "group").
    • Your enumerations aren't declared correctly for the "type" attributes. You need to either make them #IMPLIED, #REQUIRED, or give them a default value.

    Updated example prolog:

    <!DOCTYPE messages
    [
    <!ELEMENT messages (message,contact,group)>
    <!ELEMENT message (#PCDATA|transmitter|receiver)*>
    <!ELEMENT transmitter (#PCDATA)>
    <!ATTLIST message type (text|vocal|file) #REQUIRED>
    
    <!ELEMENT receiver (#PCDATA|contact|group)*>
    <!ELEMENT contact (contact_name,contact_first_name )>
    <!ELEMENT contact_name (#PCDATA)>
    <!ELEMENT contact_first_name (#PCDATA)>
    <!ELEMENT group (name_group,file_group)>
    <!ELEMENT name_group (#PCDATA)>
    <!ELEMENT file_group (#PCDATA)>
    <!ATTLIST file_group type (text|vocal|file) #REQUIRED>
    ]>