Search code examples
xmlxml-validationdtd

DTD define Number of occurrences of elements with mixed content


How we add constraints for child elements when Declaring mix content element in DTD? This is my xml.

<!ELEMENT manager (contact)>
<!ATTLIST manager name CDATA #REQUIRED>
<!ELEMENT contact (#PCDATA|office|mobile+|email*)*>
<!ELEMENT mob (#PCDATA)>
<!ELEMENT office (#PCDATA)>
<!ELEMENT email (#PCDATA)>

When I wrote this I got a error which says The mixed content model "contact" must end with ")*" when the types of child elements are constrained.

Please help..

The constraints for the contact element are, the office element must appears only one time, the mobile element must appears one or more times, the email element is optional.

This is my what I want,

<contact>
            Contact Details:
            <email>[email protected]</email>
            <mob>077769768</mob>
            <office>036568879</office>
</contact>

Solution

  • Mixed content can only be declared one way. You can't specify how many times an element can occur in a mixed content declaration.

    Your declaration would have to look like this:

    <!ELEMENT contact (#PCDATA|office|mobile|email)*>
    

    The constraints for the contact element are, the office element must appears only one time, the mobile element must appears one or more times, the email element is optional.

    Your constraints don't sound like you need mixed content; maybe this will be enough:

    <!ELEMENT contact (office,mobile+,email?)>