Reputation: 4767
I have the following basic XML and DTD:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE items [
<!ELEMENT items (item*)>
<!ELEMENT item (name, age*)>
<!ATTLIST item id ID #IMPLIED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
]>
<items>
<item id="1">
<name>Tom</name>
<age>20</age>
</item>
<item id="2">
<name>mike</name>
<age>20</age>
</item>
<item id="3">
<name>erik</name>
<age>20</age>
</item>
</items>
This validates if I change ATTLIST item id ID
to ATTLIST item id CDATA
, but currently I get the following error:
What's the issue here with the ID field? How should this be used properly?
Upvotes: 3
Views: 293
Reputation: 4767
ID
must match the name
constraint, so needs to start with a "NameStartChar", i.e., not a number. For example, this will work:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE items [
<!ELEMENT items (item*)>
<!ELEMENT item (name, gender, age*)>
<!ATTLIST item id ID #IMPLIED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT gender (#PCDATA)>
<!ELEMENT age (#PCDATA)>
]>
<items>
<item id="a1">
<name>Tom</name>
<gender>M</gender>
<age>20</age>
</item>
<item id="a2">
<name>Sarah</name>
<gender>F</gender>
<age>20</age>
</item>
</items>
Though if you need, for example, an auto-incrementing ID, just use CDATA
as the type instead.
Upvotes: 3