我想尝试在我的XML文件中添加第二个条目,但是,添加另一个条目后,我在验证器上出现错误
代码: 这是我的DTD:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE car [
<!ELEMENT car (make, model, year, colour, engine, accessories)>
<!ELEMENT make (#PCDATA)>
<!ELEMENT model (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT colour (#PCDATA)>
<!ELEMENT engine (number_of_cylinders, fuel_system)>
<!ELEMENT number_of_cylinders (#PCDATA)>
<!ELEMENT fuel_system (#PCDATA|carburetted|fueld-injected)*>
<!ELEMENT accessories EMPTY>
<!ATTLIST accessories radio (yes|no) #REQUIRED>
<!ATTLIST accessories air_conditioning (yes|no) #REQUIRED>
<!ATTLIST accessories power_steering (yes|no) #REQUIRED>
]>
XML条目:
<car>
<make>Ford</make>
<model>001</model>
<year>2014</year>
<colour>black</colour>
<engine> <number_of_cylinders>3</number_of_cylinders> <fuel_system>"carburetted"</fuel_system></engine>
<accessories radio="yes" air_conditioning="yes" power_steering="yes"/>
</car>
答案 0 :(得分:1)
您只能拥有一个根元素,因此您需要添加另一个可以包含多个car
元素的元素。
类似的东西:
<!ELEMENT cars (car+)>
我也注意到了其他一些事情。
好像你让我感到困惑internal and external subsets。您不允许在外部子集中拥有doctype声明。在您给出的示例中,应该都是一个文件。如果DTD位于单独的文件中,请将doctype声明从DTD移动到XML实例并引用DTD。
您可以通过每个元素仅使用1来简化属性声明。
您引用元素carburetted
和fueld-injected
,但不要声明它们。看起来你真的想要2个值的选项。您无法使用DTD执行此操作,因此您必须使用枚举创建属性。
以下是一些示例:
type
属性cars
元素<!DOCTYPE cars [
<!ELEMENT cars (car+)>
<!ELEMENT car (make, model, year, colour, engine, accessories)>
<!ELEMENT make (#PCDATA)>
<!ELEMENT model (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT colour (#PCDATA)>
<!ELEMENT engine (number_of_cylinders, fuel_system)>
<!ELEMENT number_of_cylinders (#PCDATA)>
<!ELEMENT fuel_system EMPTY>
<!ATTLIST fuel_system
type (carburetted|fuel-injected) #REQUIRED>
<!ELEMENT accessories EMPTY>
<!ATTLIST accessories
radio (yes|no) #REQUIRED
air_conditioning (yes|no) #REQUIRED
power_steering (yes|no) #REQUIRED>
]>
<cars>
<car>
<make>Ford</make>
<model>001</model>
<year>2014</year>
<colour>black</colour>
<engine>
<number_of_cylinders>3</number_of_cylinders>
<fuel_system type="carburetted"/>
</engine>
<accessories radio="yes" air_conditioning="yes" power_steering="yes"/>
</car>
</cars>
<!ELEMENT cars (car+)>
<!ELEMENT car (make, model, year, colour, engine, accessories)>
<!ELEMENT make (#PCDATA)>
<!ELEMENT model (#PCDATA)>
<!ELEMENT year (#PCDATA)>
<!ELEMENT colour (#PCDATA)>
<!ELEMENT engine (number_of_cylinders, fuel_system)>
<!ELEMENT number_of_cylinders (#PCDATA)>
<!ELEMENT fuel_system EMPTY>
<!ATTLIST fuel_system
type (carburetted|fuel-injected) #REQUIRED>
<!ELEMENT accessories EMPTY>
<!ATTLIST accessories
radio (yes|no) #REQUIRED
air_conditioning (yes|no) #REQUIRED
power_steering (yes|no) #REQUIRED>
<!DOCTYPE cars SYSTEM "cars.dtd">
<cars>
<car>
<make>Ford</make>
<model>001</model>
<year>2014</year>
<colour>black</colour>
<engine>
<number_of_cylinders>3</number_of_cylinders>
<fuel_system type="carburetted"/>
</engine>
<accessories radio="yes" air_conditioning="yes" power_steering="yes"/>
</car>
</cars>
答案 1 :(得分:0)
如果您的意思是想要添加另一辆车,那么您的定义需要改变。我建议根部是汽车,你可以在那里找到你的汽车元素。 DTD需要修改以指定汽车要包含的子元素数量。请参阅http://www.w3schools.com/dtd/dtd_elements.asp。