简体   繁体   中英

Writing a XML and DTD document

I am trying to tackle two of the past exam questions, I managed to finish it but I am not sure whether I have done it right. I would really appriciate if someone could help me out please. I have pasted the exam question. 在此处输入图片说明

First question requires me to write up an XML document for the TOP 3 BOOKS and this is what I got. Is the correct and would there be simpler way up doing it as I'd be required to write an XML document using pen and paper.


<Top_3_Books> 

<Book Catagory="Wine">
<Book1> 
<Title> French Wines: The Essential Guide <\Title>
<Author> Penguin Publishers <\Author>
<\Book1>

<Book2> 
<Title> An Encyclopaedia of the Wines and Domains of France <\Title>
<Author> Oxford Press <\Author>
<\Book2>

<Book3> 
<Title> Hachette Atlas of French Wines & Vineyards <\Title>
<Author> Addison-Wesley <\Author>
<\Book3>

<\Book>


<Book Catagory="Food">

<Book1> 
<Title> Seafood Recipes from Cornwall <\Title>
<Author> R.Steinway and BBC Press <\Author>
<\Book1>

<Book2> 
<Title> D. Smithson's Easy How-To-Cook <\Title>
<Author> D. Smithson and Prentice-Hall <\Author>
<\Book2>

<Book3> 
<Title> All Rhodes Lead to the Kitchen <\Title>
<Author> J. Rhodes and Addison-Wesley <\Author>
<\Book3>

<\Book>

<\Top_3_Books>

For the part c, I need to write up a DTD and below is my attempt. Is it write and again any easier or faster way. Thanks.

<!ELEMENT Top_3_Books(Wines,Food)*>
<!ELEMENT Wines (Books, Author?)>
<!ELEMENT Books (#PCDATA)>
<!ELEMENT Author (#PCDATA)>
<!ELEMENT Food (Books, Author)>
<!ELEMENT Books (#PCDATA)>
<!ELEMENT Author (#PCDATA)>

Thanks

It would seem the Wine category, for instance, should be altered a bit

a. The category is not a book. It should be a Category element, with a type. b. All books are Book items. no need to create seperate items Book1 , Book2 etc. c. Publisher attribute for every book. d. List of authors, optional. It seems this category doesn't have them, so I invented one for the last book.

<Category type="Wine">
   <Books>
      <Book Publisher="Penguin Publishers"> 
         <Title> French Wines: The Essential Guide <\Title>
         <Authors><\Authors>
      <\Book>

      <Book Publisher="Oxford Press "> 
         <Title> An Encyclopaedia of the Wines and Domains of France <\Title>
         <Authors><\Authors>
      <\Book>

      <Book Publisher="Addison-Wesley"> 
         <Title> Hachette Atlas of French Wines & Vineyards <\Title>
         <Authors>
            <Author>W. Esley</Author>
         <\Authors>
      <\Book>    
   <Books>
<\Category>

The DTD should have a DOCTYPE defining the root element.

Parenthesis define what comes in the element, so

<!DOCTYPE Top_3_Books
[
   <!ELEMENT Top_3_Books(Category)>
   <!ELEMENT Category(Books)>
   <!ATTLIST Category
      type   CDATA          #REQUIRED
   >
   <!ELEMENT Books (Book)>
   <!ELEMENT Book (Title, Authors)>
   <!ATTLIST Book 
      Publisher CDATA       #REQUIRED
   >
   <!ELEMENT Title (#PCDATA)>
   <!ELEMENT Authors (Author)*>
   <!ELEMENT Author (#PCDATA)>
]>

Here's an example that is actually valid and covers all of the requirements. (The previous answer is neither.)

<!DOCTYPE top3books [

<!--Requirement #1 - zero or more categories-->
<!ELEMENT top3books (category*)>

<!--Requirement #3 - each category has at least one book-->
<!ELEMENT category (book+)>
<!--Requirement #2 - category has a type-->
<!ATTLIST category
    type CDATA #REQUIRED>

<!--Requirements #4 and #6 - each book has a title and an optional list of authors-->
<!ELEMENT book (title, authors?)>
<!--Requirement #5 - each book has a publisher attribute-->
<!ATTLIST book 
    publisher CDATA #REQUIRED>

<!ELEMENT title (#PCDATA)>
<!ELEMENT authors (author+)>
<!ELEMENT author (#PCDATA)>
]>
<top3books>
    <category type="Wine">
        <book publisher="Penguin Publishers">
            <title>French Wines: The Essential Guide</title>
        </book>
        <book publisher="Oxford Press">
            <title>An Encyclopedia of the Wines and Domains of France</title>
        </book>
        <book publisher="Addison-Wesley">
            <title>Hachette Atlas of French Wines &amp; Vineyards</title>
        </book>
    </category>
    <category type="Food">
        <book publisher="BBC Press">
            <title>Seafood Recipes from Cornwall</title>
            <authors>
                <author>R.Steinway</author>
            </authors>
        </book>
        <book publisher="Prentice-Hall">
            <title>D. Smithson's Easy How-To-Cook</title>
            <authors>
                <author>D. Smithson</author>
            </authors>
        </book>
        <book publisher="Addison-Wesley">
            <title>All Rhodes Lead to the Kitchen</title>
            <authors>
                <author>J. Rhodes</author>
            </authors>
        </book>
    </category>
</top3books>

One of the biggest things you were missing is that your XML was not well-formed . (Specifically the end tags should use / instead of \\ and & needs to be &amp; .)

I also changed all element and attribute names to lowercase. This isn't required but remember that however you define your element is how it needs to be used (case matters).

I noticed too that you were missing a space between Top_3_Books and the content spec (Wines,Food) . Make sure you have the required space between the name and the content spec .

One additional thing; my example has the DTD in an internal subset (inside the doctype between the [ and the ] ). This means the DTD and the XML instance would all be in the same file. If you want to write the DTD into a separate file, you would need to reference the DTD in the doctype declaration from within the XML instance. Let me know if you need an example of what that would look like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM