DTD - uniqueness of ID attributes
Asked Answered
H

1

5

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?

Hahnert answered 19/9, 2013 at 8:25 Comment(0)
N
6

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.

Nootka answered 19/9, 2013 at 9:41 Comment(1)
I got additional confirmation by also validating in Oxygen XML editor (which uses Java).Nootka

© 2022 - 2024 — McMap. All rights reserved.