xmldtdxml-validation

Root element must be declared in the DTD


I have an XML document with the DTD:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE company [
        ]>
<company>
</company>

And I get the warning:

Element type "company" must be declared.

But company is the root element. Why must I declare it?


Solution

  • Because being the root element says nothing about what its content model may be. You've said company should be the top-level element; you've not said what attributes, element children, or text children it may have.

    So, either remove the DOCTYPE to create a well-formed XML document,

    <?xml version="1.0" encoding="UTF-8"?>
    <company>
    </company>
    

    or add a declaration for company such as,

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE company [
      <!ELEMENT company (#PCDATA)>
    ]>
    <company>
    </company>
    

    See also