Vijay
Vijay

Reputation: 97

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?

Upvotes: 0

Views: 1615

Answers (2)

Shobhit Solanki
Shobhit Solanki

Reputation: 1

This error is caused by the DTD file not being valid. You have created a DTD-file containing:

<!DOCTYPE catalog [
…
]>

Simply remove the first and last line delcaring the DTD data, since those are only to be used when having the DTD within your XML file. And there should be ? in climate since in plant A2 it is not present.

`

<?xml version="1.0" 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)>
 <!ATTLIST plant id CDATA #REQUIRED>

`

Upvotes: 0

kjhughes
kjhughes

Reputation: 111706

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>

Upvotes: 1

Related Questions