Joel Frick
Joel Frick

Reputation: 9

Trying to get my DTD and XML to work right

I have been trying to get my XML to not only view the DTD but I receive an error while trying to validate my DTD, stating The markup in the document preceding the root element must be well-formed. I am unsure what I am doing wrong or how to make it so that both files work. Any help would be appreciated.

DTD:

    <!ELEMENT measurements (#PCDATA|distance|weight|volume)>
    <!ELEMENT weight (#PCDATA)>
    <!ELEMENT volume (#PCDATA)>
    <!ELEMENT distance (#PCDATA)>
    <!ATTLIST distance status (metric|imperial) #REQUIRED>

XML:

    <?xml version="1.0"?>
    <!DOCTYPE measurements PUBLIC "-//American Sentinel//XML Applications//EN" "measurements.dtd">
    <measurement>
      <weight>5</weight>
      <volume>10</volume>
      <distance>metric</distance>
    </measurement>

Upvotes: 0

Views: 51

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52858

In your DTD, the declaration for measurements needs to be:

<!ELEMENT measurements (#PCDATA|distance|weight|volume)*>

This is because there is only one way to declare mixed content.

Also, you've declared measurements (plural) in your DTD, but in your XML you're using measurement (singular). You'll have to either change the DTD or the XML. If you change the DTD, don't forget to change the doctype declaration in the XML.

One other thing is that the status attribute is declared as required in your DTD.

Example of fixed DTD and XML...

DTD

<!ELEMENT measurements (#PCDATA|distance|weight|volume)*>
<!ELEMENT weight (#PCDATA)>
<!ELEMENT volume (#PCDATA)>
<!ELEMENT distance (#PCDATA)>
<!ATTLIST distance status (metric|imperial) #REQUIRED>

XML

<!DOCTYPE measurements PUBLIC "-//American Sentinel//XML Applications//EN" "measurements.dtd">
<measurements>
    <weight>5</weight>
    <volume>10</volume>
    <distance status="metric"/>
</measurements>

Upvotes: 1

Related Questions