XML - 如何添加第二个条目?

XML - How Can I Add a Second Entry?

我想尝试将第二个条目添加到我的 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>

如果您的意思是要添加另一辆车,那么您的定义需要更改。我建议根是汽车,你可以在其中找到你的汽车元素。 DTD 将需要修改以指定 cars 包含多少子元素。参见 http://www.w3schools.com/dtd/dtd_elements.asp

您只能有一个根元素,因此您需要添加另一个可以包含多个 car 元素的元素。

类似于:

<!ELEMENT cars (car+)>

我还注意到了其他一些事情。

  1. 你好像搞糊涂了internal and external subsets。您不允许在外部子集中有文档类型声明。在您给出的示例中,所有这些都应该是一个文件。如果 DTD 在单独的文件中,将 doctype 声明从 DTD 移动到 XML 实例并引用 DTD。

  2. 您可以通过每个元素仅使用 1 个来简化属性声明。

  3. 您引用了元素 carburettedfueld-injected,但没有声明它们。看起来您实际上想要 2 个值的选项。你不能用 DTD 做到这一点,所以你必须用枚举创建一个属性。

这里有几个例子可以说明:

  • 内部子集
  • 外部子集
  • 额外的元素引用转换为 type 属性
  • 属性声明简化
  • 添加 cars 元素

内部子集示例

cars.xml

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

外部子集示例

cars.dtd

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

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