问题
According to the DTD specification on the ID attribute type:
Validity constraint: 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?
- Values must differ among all instances of all attributes of type ID.
- Values must differ among all instances of the same attribute of type ID.
In other words, given the following DTD declaration snippet:
<!ELEMENT book ANY>
<!ATTLIST book id ID>
<!ELEMENT magazine ANY>
<!ATTLIST magazine id ID>
does the following XML document snippet violate the validity constraint?
<book id="ID01" />
<magazine id="ID01" />
How about if I renamed the attributes to book-id
and magazine-id
, instead of just id
in both cases?
回答1:
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>
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
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>
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
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.
来源:https://stackoverflow.com/questions/18889679/dtd-uniqueness-of-id-attributes