-2

the given code is:

<?xml version="1.0" encoding="UTF-8"?>
<student id="12C042">

  <fName>John</fName>

  <lName>Nelson</lName>

  <plan>
    <courses year="3">
        <course>
            <name> Extensible Markup Language</name>
            <shortName>XML</shortName>
            <record>
                <grade>30</grade>
                <date>12-Jan-2017</date>
            </record>
        </course>

        <course>
            <name>Object Oriented Concepts and Unified Modeling Language</name>
            <shortName><![CDATA[OOP & UML]]></shortName>
        </course>
    </courses>
  </plan>
</student>

I need to generate an DTD xml file for this.

I made a code:

<!ELEMENT student (fName,lName,plan)>
<!ELEMENT fName (#PCDATA)>
<!ELEMENT lName (#PCDATA)>
<!ELEMENT plan (courses)>
<!ELEMENT courses (course?)>
<!ELEMENT course (name,shortName*,record*)>
<!ELEMENT name ANY>
<!ELEMENT shortName ANY>
<!ELEMENT record (grade,date)>
<!ELEMENT grade ANY>
<!ELEMENT date ANY>
<!ATTLIST student id NMTOKEN #REQUIRED>
<!ATTLIST courses year CDATA #IMPLIED>

it is throwing me an error:

Error:                                                                          
   Public ID: null                                                              
   System ID: file:/home/p/student.xml                                     
   Line number: 20                                                              
   Column number: 13                                                            
   Message: The content of element type "courses" must match "(course)".        
error  
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
Arjun Gupta
  • 1
  • 1
  • 3
  • Course is defined two times in your DTD. That is obviously not allowed as the error message says. – Markus Aug 07 '17 at 10:39
  • i deleted the line <!ELEMENT course ANY> now it is showing me. Error: Public ID: null System ID: file:/home/p13290/student.xml Line number: 20 Column number: 13 Message: The content of element type "courses" must match "(course)". error – Arjun Gupta Aug 07 '17 at 10:45

3 Answers3

0

Your current declaration for courses says that course is allowed either zero or one time (that's what the ? means).

Your XML has two course elements.

You need to change the declaration to either allow course zero or more times (*) or one or more times (+).

Example...

<!ELEMENT courses (course)*>
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
0

The content of element type courses must match (course), the error is due to mismatch in the code.
This ? allow records to appear zero or one occurrences in xml, since it doesn't appear within second course tag.
So to solve this error make course dtd as <!ELEMENT course (name,shortName,record?)>.

<!ELEMENT student (fName,lName,plan)>
<!ELEMENT fName (#PCDATA)>
<!ELEMENT lName (#PCDATA)>
<!ELEMENT plan (courses)>
<!ELEMENT courses (course)>
<!ELEMENT course (name,shortName,record?)>
<!ELEMENT name ANY>
<!ELEMENT shortName ANY>
<!ELEMENT record (grade,date)>
<!ELEMENT grade ANY>
<!ELEMENT date ANY>
<!ATTLIST student id NMTOKEN #REQUIRED>
<!ATTLIST courses year CDATA #REQUIRED>
Ofek Hod
  • 3,544
  • 2
  • 15
  • 26
0

Try this, it will work

    <!ELEMENT student (fName,lName,plan*)>
    <!ELEMENT fName (#PCDATA)>
    <!ELEMENT lName (#PCDATA)>
    <!ELEMENT plan (courses*)>
    <!ELEMENT courses (course+)>
    <!ELEMENT course (name,shortName,record?)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT shortName (#PCDATA)>
    <!ELEMENT record (grade,date)>
    <!ELEMENT grade (#PCDATA)>
    <!ELEMENT date (#PCDATA)>
    <!ATTLIST student id NMTOKEN #REQUIRED>
    <!ATTLIST courses year CDATA #REQUIRED>
  • While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Brian61354270 Apr 12 '20 at 19:03