DTD-ID属性的唯一性 [英] DTD - uniqueness of ID attributes
问题描述
有效性约束:ID
Validity constraint: ID
ID类型的值必须与Name产生的值匹配。名称
不能在XML文档中多次出现。
,即ID值必须唯一地标识承载它们的元素。
Values of type ID MUST match the Name production. A name MUST NOT appear more than once in an XML document as a value of this type; i.e., ID values MUST uniquely identify the elements which bear them.
以下哪种解释正确?
Which of the following explanations is correct?
- 类型ID的所有属性的所有实例的值必须不同。
- 所有实例的值必须不同
换句话说,给定以下DTD声明代码段:
In other words, given the following DTD declaration snippet:
<!ELEMENT book ANY>
<!ATTLIST book id ID>
<!ELEMENT magazine ANY>
<!ATTLIST magazine id ID>
以下XML文档摘要是否违反了有效性约束?
does the following XML document snippet violate the validity constraint?
<book id="ID01" />
<magazine id="ID01" />
如何将属性重命名为 book-id
和 magazine-id
,而不是在两种情况下都只是 id
?
How about if I renamed the attributes to book-id
and magazine-id
, instead of just id
in both cases?
推荐答案
案例1
格式良好的XML文档:
Case 1
Well-formed XML document:
<!DOCTYPE root SYSTEM "idtest.dtd">
<root>
<book id="ID01" />
<magazine id="ID01" />
</root>
DTD:
<!ELEMENT root ANY>
<!ELEMENT book ANY>
<!ATTLIST book id ID #IMPLIED>
<!ELEMENT magazine ANY>
<!ATTLIST magazine id ID #IMPLIED>
xmllint的输出:
Output from xmllint:
$ xmllint --postvalid idtest.xml
<?xml version="1.0"?>
<!DOCTYPE root SYSTEM "idtest.dtd">
<root>
<book id="ID01"/>
<magazine id="ID01"/>
</root>
idtest.xml:4: element magazine: validity error : ID ID01 already defined
<magazine id="ID01" />
^
idtest.xml:4: element magazine: validity error : ID ID01 already defined
Document idtest.xml does not validate
案例2
格式良好的XML文档:
Case 2
Well-formed XML document:
<!DOCTYPE root SYSTEM "idtest.dtd">
<root>
<book book_id="ID01" />
<magazine magazine_id="ID01" />
</root>
DTD:
<!ELEMENT root ANY>
<!ELEMENT book ANY>
<!ATTLIST book book_id ID #IMPLIED>
<!ELEMENT magazine ANY>
<!ATTLIST magazine magazine_id ID #IMPLIED>
xmllint的输出:
Output from xmllint:
$ xmllint --postvalid idtest.xml
<?xml version="1.0"?>
<!DOCTYPE root SYSTEM "idtest.dtd">
<root>
<book book_id="ID01"/>
<magazine magazine_id="ID01"/>
</root>
idtest.xml:4: element magazine: validity error : ID ID01 already defined
<magazine magazine_id="ID01" />
^
idtest.xml:4: element magazine: validity error : ID ID01 already defined
Document idtest.xml does not validate
结论:更改属性名称没有帮助。类型很重要。 ID类型的属性值必须唯一。解释1是正确的。
Conclusion: Changing the attribute names does not help. The type is what matters. Values of attributes of type ID must be unique. Explanation 1 is correct.
这篇关于DTD-ID属性的唯一性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!