DTD - uniqueness of ID attributes

爱⌒轻易说出口 提交于 2019-12-12 13:25:05

问题


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?

  1. Values must differ among all instances of all attributes of type ID.
  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!