简体   繁体   English

编写XML和DTD文档

[英]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. 第一个问题需要我为TOP 3 BOOKS编写XML文档,这就是我得到的。 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. 是正确的,是否会有更简单的方法来完成,因为我需要使用笔和纸来编写XML文档。


<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. 对于c部分,我需要编写一个DTD,下面是我的尝试。 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 例如,似乎应该对“ Wine类别进行一些更改

a. 一种。 The category is not a book. 该类别不是一本书。 It should be a Category element, with a type. 它应该是带有类型的Category元素。 b. b。 All books are Book items. 所有书籍均为Book项目。 no need to create seperate items Book1 , Book2 etc. c. 无需创建单独的项目Book1Book2等。 Publisher attribute for every book. 每本书的Publisher属性。 d. 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. DTD应该具有定义根元素的DOCTYPE

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 . 你们中的一个人失踪最重要的事情是,你的XML并没有很好形成 (Specifically the end tags should use / instead of \\ and & needs to be &amp; .) (具体来说,结束标记应使用/代替\\并且&必须为&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) . 我也注意到您在Top_3_Books和内容规范(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 ] ). 我的示例在内部子集中(在[]之间的doctype中)具有DTD。 This means the DTD and the XML instance would all be in the same file. 这意味着DTD和XML实例都将在同一文件中。 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. 如果要将DTD写入单独的文件中,则需要在XML实例中的doctype声明中引用DTD。 Let me know if you need an example of what that would look like. 让我知道您是否需要一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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