Marnix
Marnix

Reputation: 6547

How to declare empty or not-empty dtd element

How can I declare an element in DTD that is self-closing or contains elements? I have found the *-operator, but I can't verify if this can also validate empty elements.

I have tried this, but it gives a compilation error in Visual Studio saying that the EMPTY element is not declared:

<!ELEMENT File (Annotations|EMPTY)>
<!ELEMENT Annotations (State*)>
<!ELEMENT State EMPTY>

Or I could try the following, but I can't validate if it is ok:

<!ELEMENT File (Annotations?)>
...

Upvotes: 4

Views: 5576

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52888

Yes, your element declaration for File is correct:

<!ELEMENT File (Annotations?)>

What you're saying is that File can contain zero or one Annotations element.

Also, if you would've used * instead of ?, you would've been saying File can contain zero or more Annotations elements.

Valid examples:

<!DOCTYPE File [
<!ELEMENT File (Annotations?)>
<!ELEMENT Annotations (State*)>
<!ELEMENT State EMPTY>
]>
<File/>

<!DOCTYPE File [
<!ELEMENT File (Annotations?)>
<!ELEMENT Annotations (State*)>
<!ELEMENT State EMPTY>
]>
<File></File>

<!DOCTYPE File [
<!ELEMENT File (Annotations?)>
<!ELEMENT Annotations (State*)>
<!ELEMENT State EMPTY>
]>
<File>
  <Annotations/>
</File>

Upvotes: 2

Related Questions