jonathan
jonathan

Reputation: 291

how to declare an element inside an element with the same name in DTD for XML?

suppose we have a root element called first

now first has 2 sub elements called second and third or just text(#PCDATA)

the second should always be declared before the third element

now, the second sub element can have another(ONLY ONE) second sub element or just text(#PCDATA)

the third sub element can only have text(#PCDATA)

So we can have something like this:

      <first>
           <second> I'm in second </second>
           <third> I'm in third </third>
      </first>

or something like this:

      <first>
           <second> 
                <second> I'm in second </second>
           </second>
           <third> I'm in third </third>
      </first>

the difficult thing here is, how to make it so that first second element will only be able to accept ONE second sub element although the second sub element is a second element as well, it won't be able to accept other elements but text, because it is already a sub element of another element

I've tried creating something like this

    <!DOCTYPE first [

        <!ELEMENT first (second,third)>
        <!ELEMENT second (#PCDATA | second, third)>
        <!ELEMENT third (#PCDATA)>
     ]>

but unfortunately this doesn't work at all

I'm using this validator: http://validator.w3.org/check

and for this code

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

    <!DOCTYPE first [

        <!ELEMENT first (second,third)>
        <!ELEMENT second (#PCDATA | second, third)>
        <!ELEMENT third (#PCDATA)>
     ]>

      <first>
           <second> I'm in second </second>
           <third> I'm in third </third>
      </first>

it gives me errors

any ideas?

Upvotes: 1

Views: 1386

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52888

Whenever you have a mixed content model (both PCDATA and other elements), you can't specify order with the , ("followed by") sequence operator/connector.

You have to use the | ("or") sequence operator/connector. You also can't mix , and | within the same group. You must also use the * ("zero or more") occurrence indicator.

Example

<!DOCTYPE first [
<!ELEMENT first (second,third)>
<!ELEMENT second (#PCDATA|second|third)*>
<!ELEMENT third (#PCDATA)>
]>

Upvotes: 2

Related Questions