Menu

DTD Question.

Help
James
2008-07-02
2015-09-23
  • James

    James - 2008-07-02

    Hi im fairly new to this and trying to write a dtd for my xml document.

    I have written this:

    <!--This is a DTD for the first home work-->
    <!--This document was constructed by James Kester-->
    <!--Homework2 CS366 6/1/08-->
    <!ELEMENT courses (course+)>
    <!ELEMENT course (title,section+)>
    <!ATTLIST course number ID #REQUIRED>
    <!ATTLIST course credits CDATA #REQUIRED>
    <!ELEMENT title (#PCDATA)>
    <!ELEMENT section (enrollment,date,days?,time?,room?,instructor+)>
    <!ATTLIST section number CDATA #REQUIRED>
    <!ATTLIST section id ID #REQUIRED>
    <!ATTLIST section delivery (classroom | on-line | ITV) #REQUIRED>
    <!ELEMENT enrollment (#PCDATA)>
    <!ELEMENT date (start_date,end_date)>
    <!ELEMENT start_date (#PCDATA)>
    <!ELEMENT end_date (#PCDATA)>
    <!ELEMENT days (#PCDATA)>
    <!ELEMENT time (start_time,end_time)>
    <!ELEMENT start_time (#PCDATA)>
    <!ELEMENT end_time (#PCDATA)>
    <!ELEMENT room (#PCDATA)>
    <!ATTLIST room campus CDATA #REQUIRED>
    <!ELEMENT instructor (first,last)>
    <!ELEMENT first (#PCDATA)>
    <!ELEMENT last (#PCDATA)>

    when i check its well formedness it tells me there is a syntax error on the first element "courses"

    Error at line 4, column 1: syntax error

    am I doing something wrong?

     
    • gnschmidt

      gnschmidt - 2008-07-02

      The reason is that DTDs by themselves aren't XML unless they are embedded in an XML document.

      I agree, though, that it would be useful to implement the well-formedness check in such a way that it realises that it's dealing with a DTD and checks it using a parser. (If you check the well-formedness of a document that is attached to your DTD, the well-formedness check will automatically flag any problems with your DTD.)

      Please feel free to log this as a feature request in the tracker.

      Regards,
      Gerald

       
    • James

      James - 2008-07-02

      well it is an external DTD for an XML document I have written maybe but I would like to validate the 2 but i cant perhaps the XML document would help too here it is:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE courses SYSTEM "homework2.dtd">
      <jk:courses xmlns:jk="http://www.jameskester.com">
      <jk:course number="cs341" credits="4">
      <jk:title>Data Structures</jk:title>
      <jk:section number="01" id="X001780" delivery="Classroom">
      <jk:enrollment>4</jk:enrollment>
      <jk:date>
      <jk:start_date>08/26/2008</jk:start_date>
      <jk:end_date>12/11/08</jk:end_date>
      </jk:date>
      <jk:days>T,TH</jk:days>
      <jk:time>
      <jk:start_time>4:00pm</jk:start_time>
      <jk:end_time>5:50pm</jk:end_time>
      </jk:time>
      <jk:room campus="Rochester">EA244</jk:room>
      <jk:instructor>
      <jk:first>Nicole</jk:first>
      <jk:last>Anderson</jk:last>
      </jk:instructor>
      </jk:section>
      <jk:section number="02" id="X001788" delivery="Online">
      <jk:enrollment>0</jk:enrollment>
      <jk:date>
      <jk:start_date>08/29/2008</jk:start_date>
      <jk:end_date>12/12/08</jk:end_date>
      </jk:date>
      <jk:room campus="Rochester"></jk:room>
      <jk:instructor>
      <jk:first>Nicole</jk:first>
      <jk:last>Anderson</jk:last>
      </jk:instructor>
      <jk:instructor>
      <jk:first>Chi-Cheng</jk:first>
      <jk:last>Lin</jk:last>
      </jk:instructor>
      </jk:section>
      </jk:course>
      <jk:course number="cs368" credits="3">
      <jk:title>Introduction to Bioinformatics</jk:title>
      <jk:section number="01" id="X001781" delivery="ITV">
      <jk:enrollment>3</jk:enrollment>
      <jk:date>
      <jk:start_date>08/27/2008</jk:start_date>
      <jk:end_date>12/10/08</jk:end_date>
      </jk:date>
      <jk:time>
      <jk:start_time>6:00pm</jk:start_time>
      <jk:end_time>8:40pm</jk:end_time>
      </jk:time>
      <jk:room campus="Rochester">AT102</jk:room>
      <jk:instructor>
      <jk:first>Chi-Cheng</jk:first>
      <jk:last>Lin</jk:last>
      </jk:instructor>
      <jk:instructor>
      <jk:first>Mingrui</jk:first>
      <jk:last>Zhang</jk:last>
      </jk:instructor>
      </jk:section>
      </jk:course>
      <jk:course number="cs375" credits="4">
      <jk:title>Computer Systems</jk:title>
      <jk:section number="01" id="X001782" delivery="Classroom">
      <jk:enrollment>12</jk:enrollment>
      <jk:date>
      <jk:start_date>08/25/2008</jk:start_date>
      <jk:end_date>12/10/08</jk:end_date>
      </jk:date>
      <jk:days>M,W</jk:days>
      <jk:time>
      <jk:start_time>4:00pm</jk:start_time>
      <jk:end_time>5:50pm</jk:end_time>
      </jk:time>
      <jk:room campus="Rochester">EA244</jk:room>
      <jk:instructor>
      <jk:first>Chi-Cheng</jk:first>
      <jk:last>Lin</jk:last>
      </jk:instructor>
      </jk:section>
      </jk:course>
      <jk:course number="cs415" credits="3">
      <jk:title>Principles of Programming Language</jk:title>
      <jk:section number="01" id="X001784" delivery="Classroom">
      <jk:enrollment>13</jk:enrollment>
      <jk:date>
      <jk:start_date>08/26/2008</jk:start_date>
      <jk:end_date>12/11/08</jk:end_date>
      </jk:date>
      <jk:days>T,TH</jk:days>
      <jk:time>
      <jk:start_time>4:00pm</jk:start_time>
      <jk:end_time>5:20pm</jk:end_time>
      </jk:time>
      <jk:room campus="Rochester">EA246</jk:room>
      <jk:instructor>
      <jk:first>Hugh</jk:first>
      <jk:last>Ouellette</jk:last>
      </jk:instructor>
      </jk:section>
      </jk:course>
      <jk:course number="cs385" credits="3">
      <jk:title>Applied Database Management Systems</jk:title>
      <jk:section number="01" id="X001783" delivery="Classroom">
      <jk:enrollment>6</jk:enrollment>
      <jk:date>
      <jk:start_date>08/26/2008</jk:start_date>
      <jk:end_date>12/11/08</jk:end_date>
      </jk:date>
      <jk:days>T,TH</jk:days>
      <jk:time>
      <jk:start_time>11:00am</jk:start_time>
      <jk:end_time>12:20am</jk:end_time>
      </jk:time>
      <jk:room campus="Rochester">ST108</jk:room>
      <jk:instructor>
      <jk:first>Nicole</jk:first>
      <jk:last>Anderson</jk:last>
      </jk:instructor>
      </jk:section>
      </jk:course>
      </jk:courses>

      <!--This is an XML document constructed by James Kester-->

       
    • gnschmidt

      gnschmidt - 2008-07-02

      Hi James, the DTD is fine, but the document isn't valid because of the namespace prefixes.

      The namespace declaration xmlns:jk="http://www.jameskester.com" is just another attribute as far as the DTD is concerned. DTDs have no concept of namespaces, so the 'jk:' prefix is just part of the element name. If you:

      1. add 'jk:' to all element names in your DTD
      2. change the delivery attribute enumeration values to upper case
      3. add an ATTLIST for the attribute xmlns:jk

      the document validates fine.

      Here is the modified DTD:

      <!--This is a DTD for the first home work-->
      <!--This document was constructed by James Kester-->
      <!--Homework2 CS366 6/1/08-->
      <!ELEMENT jk:courses (jk:course+)>
      <!ATTLIST jk:courses xmlns:jk CDATA #REQUIRED>
      <!ELEMENT jk:course (jk:title,jk:section+)>
      <!ATTLIST course number ID #REQUIRED>
      <!ATTLIST course credits CDATA #REQUIRED>
      <!ELEMENT jk:title (#PCDATA)>
      <!ELEMENT jk:section (jk:enrollment,jk:date,jk:days?,jk:time?,jk:room?,jk:instructor+)>
      <!ATTLIST jk:section number CDATA #REQUIRED>
      <!ATTLIST jk:section id ID #REQUIRED>
      <!ATTLIST jk:section delivery (Classroom | Online | ITV) #REQUIRED>
      <!ELEMENT jk:enrollment (#PCDATA)>
      <!ELEMENT jk:date (jk:start_date,jk:end_date)>
      <!ELEMENT jk:start_date (#PCDATA)>
      <!ELEMENT jk:end_date (#PCDATA)>
      <!ELEMENT jk:days (#PCDATA)>
      <!ELEMENT jk:time (jk:start_time,jk:end_time)>
      <!ELEMENT jk:start_time (#PCDATA)>
      <!ELEMENT jk:end_time (#PCDATA)>
      <!ELEMENT jk:room (#PCDATA)>
      <!ATTLIST room campus CDATA #REQUIRED>
      <!ELEMENT jk:instructor (jk:first,jk:last)>
      <!ELEMENT jk:first (#PCDATA)>
      <!ELEMENT jk:last (#PCDATA)>

      If you have any questions or suggestions, please don't hesitate to ask.

      Regards,
      Gerald

       
    • James

      James - 2008-07-02

      thank you very much I had an idea that it had to do with the namespace, but I couldnt find any good info on it.

      The xml document now validates, but i still have a syntax error on the DTD and it says it is not well formed. Do DTDs have to be well formed?

       
      • gnschmidt

        gnschmidt - 2008-07-02

        Hi James, I've got to rush off in a minute, but in a nutshell: if a document attached to an external DTD is valid, the DTD is certain to be sound/correct/free from errors. It means that the document and DTD together are well-formed and that, additionally, the document is valid with regard to the DTD.

        Essentially a DTD can only ever be well-formed in association with an XML document that surrounds or is attached to it.

        Sorry if this doesn't make a lot of sense, I've got to run! Will be back at the computer later working on the Win version of 1.1.0.7 though ;-)

        Best,
        Gerald

         
    • James

      James - 2008-07-02

      It makes sense Thank you Gerald, Im now attempting to create an XSD for the same XML file im sure i will have more questions.

      Thank you again.

       
      • gnschmidt

        gnschmidt - 2008-07-02

        That makes perfect sense. I think it will be interesting to place the XSD alongside the DTD to show that, for namespace-based vocabularies, XSD is a much better choice.

        Best,
        Gerald

         
  • sabah suhail

    sabah suhail - 2015-09-23

    Hello every one
    I have just started to work in xml. I have written the following xml and dtd files in xml copy editor.

    <!ELEMENT head (from,to,subject)>
    <!ELEMENT from EMPTY>
    <!ATTLIST from
    name CDATA #IMPLIED
    address CDATA #IMPLIED >
    ] >
    <email> <from name="com" address="sabah@abc.com">
    <to name="com2" address="sabah@@gmail.com">
    <subject> Greetings</subject>
    </to></from></email>

    I hope you are enjoying working in XML I can do it very well


    when i run dtd i get the following error
    no element found and the line mentioned is here ]>
    please tell me what to do

     
  • sabah suhail

    sabah suhail - 2015-09-23

    if i remove <!DOCTYPE email then it gives syntax error in line <!ELEMENT email ( head, body )>

     

Log in to post a comment.