Search code examples
xmldtdxml-validation

What is wrong with my XML file with an internal DTD subset?


I'm getting markup declaration error at the atlist declaration line in the following XML file:

<?xml encoding="UTF-8"?>
<!ELEMENT catalog (title,(plant)+)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT plant ((name)+,(climate)+,(height)+,(usage)+,(image)+)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT climate (#PCDATA)>
<!ELEMENT height (#PCDATA)>
<!ELEMENT usage (#PCDATA)>
<!ELEMENT image (#PCDATA)>
<!ATLIST plant id CDATA #REQUIRED>

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

<!DOCTYPE catalog SYSTEM "plantdtd.dtd">

<catalog>

<title>Flowers of the week</title>

<plant id="A1">

    <name>Aloe vera</name>

    <climate>tropical</climate>

    <height>60-100cm</height>

    <usage>medicinal</usage>

    <image>aloevera.jpg</image>

</plant>

<plant id="A2">

    <name>Orchidaceae</name>

    <height>8-12in</height>

    <usage>medicinal</usage>

    <usage>decoration</usage>

    <image>Orchidaceae.jpg</image>

</plant>

</catalog>

What is wrong with my XML document?


Solution

  • Your XML document has both well-formedness and validity problems...

    Problems preventing your XML document from being well-formed, including:

    • There internal DTD subset syntax is not constructed properly.
    • There are multiple XML declarations.
    • ATLIST should be ATTLIST

    Problem preventing your XML document from being valid:

    • The A2 plant has to have at least one climate child element.

    The following XML is corrected to be well-formed and valid:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE catalog [
    <!ELEMENT catalog (title,(plant)+)>
    <!ELEMENT title (#PCDATA)>
    <!ELEMENT plant ((name)+,(climate)+,(height)+,(usage)+,(image)+)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT climate (#PCDATA)>
    <!ELEMENT height (#PCDATA)>
    <!ELEMENT usage (#PCDATA)>
    <!ELEMENT image (#PCDATA)>
    <!ATTLIST plant id CDATA #REQUIRED>
    ]>
    
    <catalog>
      <title>Flowers of the week</title>
      <plant id="A1">
        <name>Aloe vera</name>
        <climate>tropical</climate>
        <height>60-100cm</height>
        <usage>medicinal</usage>
        <image>aloevera.jpg</image>
      </plant>
      <plant id="A2">
        <name>Orchidaceae</name>
        <climate/>
        <height>8-12in</height>
        <usage>medicinal</usage>
        <usage>decoration</usage>
        <image>Orchidaceae.jpg</image>
      </plant>
    </catalog>