编写XML和DTD文档

时间:2015-04-26 12:11:12

标签: xml tags element document dtd

我正在尝试解决过去的两个考题,我设法完成了但我不确定我是否做得对。如果有人可以帮助我,我会非常高兴。我已经粘贴了考试题目。 enter image description here

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

对于c部分,我需要写一个DTD,下面是我的尝试。它是否写得更容易或更快。感谢。

<!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)>

由于

2 个答案:

答案 0 :(得分:0)

例如,Wine类似乎应该稍微改变一下

一个。该类别不是一本书。它应该是Category元素,具有类型。 湾所有图书都是Book项。无需创建单独的项Book1Book2等。 C。每本书的Publisher属性。 d。作者列表,可选。看来这个类别没有它们,所以我为最后一本书发明了一个。

<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>

DTD应该有一个DOCTYPE来定义根元素。

括号定义元素中的内容,所以

<!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)>
]>

答案 1 :(得分:0)

这是一个实际有效的示例,涵盖了所有要求。 (之前的答案都不是。)

<!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>

您遗失的最大问题之一是您的XML不是well-formed。 (具体而言,结束标记应使用/而不是\&需要&amp;。)

我还将所有元素和属性名称更改为小写。这不是必需的,但请记住,无论如何定义元素都需要使用它(大小写很重要)。

我也注意到您错过了Top_3_Books和内容规范(Wines,Food)之间的空格。确保您拥有required space between the name and the content spec

另外一件事;我的示例在内部子集中有DTD(在[]之间的doctype内)。这意味着DTD和XML实例都将在同一个文件中。如果要将DTD写入单独的文件,则需要在XML实例中引用doctype声明中的DTD。如果您需要一个示例,请告诉我。