Results 1 to 7 of 7

Thread: Help Writing XML Document

  1. #1
    Join Date
    May 2010
    Posts
    3,852
    Rep Power
    17

    Exclamation Help Writing XML Document

    I need help getting my XML document to validate. I wrote my own DTD so I am building the XML document from that DTD.

    The DTD is based on the folowing

    Create a DTD for courses in a university. Each course element should have child elements department, course_name, course_number, instructor, course_type, time, and room. The course_type element must have attributes lab and lecture, each of which may have values true or false.

    DTD FILE

    Code:
    <!ELEMENT courses (department, course_name, course_number, instructor, course_type, time,room)>
    <!ELEMENT department (#PCDATA)>
    <!ELEMENT course_name (#PCDATA)>
    <!ELEMENT course_number (#PCDATA)>
    <!ELEMENT instructor (#PCDATA)>
    <!ELEMENT course_type (lab,lecture)> 
    <!ELEMENT time (#PCDATA)> 
    <!ELEMENT room (#PCDATA)> 
    <!ATTLIST course_type lab CDATA "true | false">
    <!ATTLIST course_type lecture CDATA "true | false">

    XML FILE

    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
         <!DOCTYPE courses SYSTEM "http://###############/~########### >
    
    
    <courses>
    
    <department>Computer Science</department>
    <course_name>Programming I</course_name>
    <course_number>CS 3332</course_number>
    <instructor>Vande Van</instructor>
    <course_type> </course_type>
    <time>1pm</time>
    <room>J-256</room>
    
    <department>Computer Science</department>
    <course_name>Programming II</course_name>
    <course_number>CS 3333</course_number>
    <instructor> Maxwell</instructor>
    <course_type> </course_type>
    <time>3pm</time>
    <room>J-260</room>
    
    <department>Information Technology</department>
    <course_name>Web Development</course_name>
    <course_number>IT 3203</course_number>
    <instructor> Brown</instructor>
    <course_type></course_type>
    <time>3pm</time>
    <room>J-210</room>
    </courses>
    The font in red is where the validator is giving an error, i left it empty on purpose

    Here is a validator
    Last edited by King_Jay16; Mar 30, 2012 at 12:11 AM.

  2. #2
    Join Date
    Apr 2003
    Posts
    13,270
    Rep Power
    35

    Default

    Quote Originally Posted by King_Jay16 View Post
    The font in red is where the validator is giving an error,
    Care to share the error? I'm good at guessing but it might take me a while.

    Edit:
    Ok, managed to validate both DTD and the XML file together. Got the error:
    Line 22: Content of element type "course_type" is incomplete, it must match "(lab,lecture)".
    Error Position:<course_type> </course_type>
    Last edited by Arch_Angel; Mar 30, 2012 at 12:29 AM.
    "The best software is the one that fits your needs." - A_A

    Virus free since: date unknown
    Anti-virus free since: August 2008

  3. #3
    Join Date
    May 2010
    Posts
    3,852
    Rep Power
    17

    Default

    Quote Originally Posted by Arch_Angel View Post
    Care to share the error? I'm good at guessing but it might take me a while.
    Last edited by King_Jay16; Mar 30, 2012 at 10:37 AM.

  4. #4
    Join Date
    Apr 2003
    Posts
    13,270
    Rep Power
    35

    Default

    It seems to me Jay, that the problem lies with your DTD. Specifically:
    Code:
    <!ELEMENT course_type (lab,lecture)>
    I don't have a lot of experience with XML, I just know the basics of it. But by reading this tuturial: http://www.w3schools.com/dtd/dtd_elements.asp
    it seems your course_type element is the problem.

    If you declare it like that in the DTD then in your document it should look like:
    Code:
    ...
    <course_type>
    <lab></lab>
    <lecturer></lecturer>
    </course_type>
    At least, that is what I understood from the tuturial.

    Elements with Children (sequences)
    Elements with one or more children are declared with the name of the children elements inside parentheses:
    Code:
    <!ELEMENT element-name (child1)>
    or
    <!ELEMENT element-name (child1,child2,...)>
    
    Example:
    
    <!ELEMENT note (to,from,heading,body)>
    When children are declared in a sequence separated by commas, the children must appear in the same sequence in the document. In a full declaration, the children must also be declared, and the children can also have children.
    Just trying to help you fix the problem.

    EDIT: So playing with the code some more, the way you specified the lab and lecture as child elements of course_type, I added them into your XML file.
    You also have lab and lecture specified as attributes for the element course_type, soooo, your XML file should look like this based on your DTD.

    Code:
    <!DOCTYPE courses [
    <!ELEMENT courses (department, course_name, course_number, instructor, course_type, time,room)>
    <!ELEMENT department (#PCDATA)>
    <!ELEMENT course_name (#PCDATA)>
    <!ELEMENT course_number (#PCDATA)>
    <!ELEMENT instructor (#PCDATA)>
    <!ELEMENT course_type (lab,lecture)>
    <!ELEMENT lab (#PCDATA)>
    <!ELEMENT lecture (#PCDATA)>
    <!ELEMENT time (#PCDATA)> 
    <!ELEMENT room (#PCDATA)> 
    <!ATTLIST course_type lab CDATA "true | false">
    <!ATTLIST course_type lecture CDATA "true | false">
    ]>
    
    <courses>
    
    <department>Computer Science</department>
    <course_name>Programming I</course_name>
    <course_number>CS 3332</course_number>
    <instructor>Vande Van</instructor>
    <course_type lab="true" lecture="true">
    <lab></lab>
    <lecture></lecture>
    </course_type>
    <time>1pm</time>
    <room>J-256</room>
    ...
    ...
    Not sure what you intended for the lab and lecture elements. If you wanted them as attributes or as child elements. But makes the changes as necessary.
    Last edited by Arch_Angel; Mar 30, 2012 at 01:12 AM.
    "The best software is the one that fits your needs." - A_A

    Virus free since: date unknown
    Anti-virus free since: August 2008

  5. #5
    Join Date
    May 2010
    Posts
    3,852
    Rep Power
    17

    Default

    Thanks!! I got it solved!! Was still missing something from the DTD and XML :


    XML
    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
         <!DOCTYPE courses SYSTEM "http://" >
    
    
    <courses>
    <course> /// Was missing from each block
    <department>Computer Science</department>
    <course_name>Programming I</course_name>
    <course_number>CS 3332</course_number>
    <instructor>Vande Van</instructor>
    <course_type lab="true" lecture="true">
    <lab>false</lab>
    <lecture>true</lecture>
    </course_type>
    <time>1pm</time>
    <room>J-256</room>
    </course>
    
    <course>
    <department>Computer Science</department>
    <course_name>Programming II</course_name>
    <course_number>CS 3333</course_number>
    <instructor> Maxwell</instructor>
    <course_type lab="true" lecture="true">
    <lab>true</lab>
    <lecture>false</lecture>
    </course_type>
    <time>3pm</time>
    <room>J-260</room>
    </course>
    
    <course>
    <department>Information Technology</department>
    <course_name>Web Development</course_name>
    <course_number>IT 3203</course_number>
    <instructor> Brown</instructor>
    <course_type lab="true" lecture="true">
    <lab>true</lab>
    <lecture>true</lecture>
    </course_type>
    <time>3pm</time>
    <room>J-210</room>
    </course>
    
    
    </courses>


    DTD



    Code:
    <!ELEMENT courses (course+)>///Was missing, allows duplicate entry
    <!ELEMENT course (department, course_name, course_number, instructor, course_type, time,room)>
    <!ELEMENT department (#PCDATA)>
    <!ELEMENT course_name (#PCDATA)>
    <!ELEMENT course_number (#PCDATA)>
    <!ELEMENT instructor (#PCDATA)>
    <!ELEMENT course_type (lab,lecture)>
    <!ELEMENT lab (#PCDATA)>
    <!ELEMENT lecture (#PCDATA)>
    <!ELEMENT time (#PCDATA)> 
    <!ELEMENT room (#PCDATA)> 
    <!ATTLIST course_type lab CDATA "true | false">
    <!ATTLIST course_type lecture CDATA "true | false">


    The lab or lecture section is used to tell whether a class is a lab or lecture.
    Last edited by King_Jay16; Mar 30, 2012 at 11:38 AM.

  6. #6
    Join Date
    Apr 2003
    Posts
    13,270
    Rep Power
    35

    Default

    Excellent!

    Now I need to get back to some C++ programming.
    "The best software is the one that fits your needs." - A_A

    Virus free since: date unknown
    Anti-virus free since: August 2008

  7. #7
    Join Date
    May 2010
    Posts
    3,852
    Rep Power
    17

    Default

    Quote Originally Posted by Arch_Angel View Post
    Excellent!

    Now I need to get back to some C++ programming.
    lol.. I did that back in 2010 at Utech... C and C++ in one... did C# last semester at my current university.... was pretty easy especially with Visual Studio..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •