XML - DTD,两个元素相同的属性

时间:2013-12-19 18:07:33

标签: xml dtd

为什么以下代码不正确? XMLspy在元素employee

中发现错误
<!ELEMENT personnel (contractor*,employee*,partner*)+ >
<!ELEMENT contractor (info)>
<!ELEMENT employee(info)>
<!ATTLIST info lname CDATA #REQUIRED fname CDATA #REQUIRED hiredate CDATA #REQUIRED firedate CDATA #REQUIRED>

1 个答案:

答案 0 :(得分:1)

employee后需要一个空格。

info元素也未定义。

如果你要做的是获取contractoremployee上的属性,那么就这样做了:

<!ELEMENT personnel (contractor*, employee*, partner*)+ >
<!ELEMENT contractor EMPTY>
<!ELEMENT employee EMPTY>
<!ATTLIST contractor lname CDATA #REQUIRED fname CDATA #REQUIRED hiredate CDATA #REQUIRED firedate CDATA #REQUIRED>
<!ATTLIST employee lname CDATA #REQUIRED fname CDATA #REQUIRED hiredate CDATA #REQUIRED firedate CDATA #REQUIRED>

根据上述DTD,这是有效的:

<personnel>
  <employee lname="Foo" fname="Bar" firedate="1" hiredate="2"/>
  <contractor lname="Foo" fname="Bar" firedate="1" hiredate="2"/>
</personnel>

或者你可以像这样分享属性声明:

<!ENTITY % shared "lname CDATA #REQUIRED fname CDATA #REQUIRED hiredate CDATA #REQUIRED firedate CDATA #REQUIRED">
<!ELEMENT personnel (contractor*, employee*, partner*)+ >
<!ELEMENT contractor EMPTY>
<!ELEMENT employee EMPTY>
<!ATTLIST contractor %shared;>
<!ATTLIST employee %shared;>

这样,如果您需要更改属性列表,则contractoremployee都会更改。