如何声明DTD元素?

时间:2017-04-12 16:51:22

标签: xml dtd

我正在完成一项任务并获得了我的结果,但似乎我忘了在我的DTD文件中声明这些元素。我是否声明所有元素或仅包含额外信息的元素?如果我不清楚,请参阅我的作业摘录:

<entry id= 'c01'>
<MetaTags>Business</MetaTags>   
<title><brand>HP Pavilion</brand><name>550-112NA</name></title>
<Desciption>While other towers have been standing still, HP has revolutionized the category. From magnified performance and reliability, to its stylish redesign, this HP Pavilion is the best thing to happen to towers in over 20 years.</Desciption>
<Price>€579</Price>
<Image src ="Image1.jpg"/>
<Specs>
    <CPU>A10-8750 APU</CPU>
    <GPU>Radeon R7</GPU>
    <RAM>8 GB DDR3</RAM>
    <Storage><HDD> 2TB </HDD><SSD></SSD></Storage>
    <OS>Windows 10</OS>
    <optional>
        <Monitor>LG 22" Full HD TV</Monitor>
        <Keyboard>Microsoft Wired Keyboard 600</Keyboard>
        <Mouse>Logitech M705 Mouse</Mouse>              
        </optional>
    </Specs>
</entry>

是否必须声明标记条目,但是没有其他元素可以,因为它们没有其他变量?

如果这是正确的,声明是否会如下:

 <!ATTLIST entry id CDATA #REQUIRED> 

我希望我能清楚地表达我的问题,因为我是XML的新手。如果你需要看到这个问题,我的DTD就是这样,如果我做错了什么的话。谢谢!

<!ELEMENT ComputerShop (entry+)>
<!ELEMENT entry (MetaTags, title, Description, Price, Image, Specs)>
<!ELEMENT MetaTags (#PCDATA)>
<!ELEMENT Description (#PCDATA)>
<!ELEMENT Price (#PCDATA)>
<!ELEMENT Image (#PCDATA)>
<!ELEMENT title (brand, name)>
<!ELEMENT brand (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT Specs (CPU, GPU, RAM, Storage, OS, optional)>
<!ELEMENT CPU (#PCDATA)>
<!ELEMENT GPU (#PCDATA)>
<!ELEMENT RAM (#PCDATA)>
<!ELEMENT Storage (HDD | SSD)>
<!ELEMENT OS (#PCDATA)>
<!ELEMENT optional (Monitor | Keyboard | Mouse>
<!ELEMENT Monitor (#PCDATA)>
<!ELEMENT Keyboard (#PCDATA)>
<!ELEMENT Mouse (#PCDATA)>

谢谢!

1 个答案:

答案 0 :(得分:1)

  

...我忘了在我的DTD文件中声明元素

我认为你的意思是&#34;我忘了在我的DTD文件中声明属性&#34;。

必须声明任何属性(&#34;额外信息&#34;元素)。

因此,您需要在id元素上声明entry属性,并在src元素上声明Image属性。

以下是您需要进行的其他更改...

DTD更改

  • <!ELEMENT Specs (CPU, GPU, RAM, Storage, OS, optional)>

  • 中缺少结束工作
  • 声明HDD和SSD元素:<!ELEMENT HDD (#PCDATA)><!ELEMENT SDD (#PCDATA)>

  • optional的声明需要改变。也许到<!ELEMENT optional (Monitor | Keyboard | Mouse)*>(零或多次出现的监视器,键盘或鼠标(按任意顺序))或<!ELEMENT optional (Monitor?, Keyboard?, Mouse?)>(零或一个监视器后跟零或一个键盘后跟零或一个鼠标)

XML更改

  • Description拼错为Desciption

  • Storage的内容是一个HDD或一个SSD;不是都。删除空的<SSD></SSD>

以下是更正后的文件......

<强> DTD

<!ELEMENT ComputerShop (entry+)>
<!ELEMENT entry (MetaTags, title, Description, Price, Image, Specs)>
<!ATTLIST entry id CDATA #REQUIRED> 
<!ELEMENT MetaTags (#PCDATA)>
<!ELEMENT Description (#PCDATA)>
<!ELEMENT Price (#PCDATA)>
<!ELEMENT Image (#PCDATA)>
<!ATTLIST Image src CDATA #REQUIRED>
<!ELEMENT title (brand, name)>
<!ELEMENT brand (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT Specs (CPU, GPU, RAM, Storage, OS, optional)>
<!ELEMENT CPU (#PCDATA)>
<!ELEMENT GPU (#PCDATA)>
<!ELEMENT RAM (#PCDATA)>
<!ELEMENT Storage (HDD | SSD)>
<!ELEMENT HDD (#PCDATA)>
<!ELEMENT SDD (#PCDATA)>
<!ELEMENT OS (#PCDATA)>
<!ELEMENT optional (Monitor | Keyboard | Mouse)*>
<!ELEMENT Monitor (#PCDATA)>
<!ELEMENT Keyboard (#PCDATA)>
<!ELEMENT Mouse (#PCDATA)>

<强> XML

<entry id='c01'>
    <MetaTags>Business</MetaTags>   
    <title><brand>HP Pavilion</brand><name>550-112NA</name></title>
    <Description>While other towers have been standing still, HP has revolutionized the category. From magnified performance and reliability, to its stylish redesign, this HP Pavilion is the best thing to happen to towers in over 20 years.</Description>
    <Price>€579</Price>
    <Image src="Image1.jpg"/>
    <Specs>
        <CPU>A10-8750 APU</CPU>
        <GPU>Radeon R7</GPU>
        <RAM>8 GB DDR3</RAM>
        <Storage><HDD> 2TB </HDD></Storage>
        <OS>Windows 10</OS>
        <optional>
            <Monitor>LG 22" Full HD TV</Monitor>
            <Keyboard>Microsoft Wired Keyboard 600</Keyboard>
            <Mouse>Logitech M705 Mouse</Mouse>              
        </optional>
    </Specs>
</entry>