I’m new to XML and XSD and I have a question about XML-Schema validation. Consider XML file:
<RequestList>
<Request1 id="1">...</Request1>
<Request2 id="2">...</Request2>
.
.
.
</RequestList>
I want to force id attribute to be unique. Google helped me much in case that elements have the same type. But I can’t make a sequence of sinlge element ‘Request’, because each request can contain different information, according to his type. What should I do?
Thanks in advance for help.
The XML spec defines a unique ID type:
The Name production is
This says that if an attribute is declared as of type ID (whether in DTD or XMLSchema) then the validator must ensure that it is unique within the document. Note that the ID must start with a letter or underscore so pure numbers won’t work.
Personally I don’t use ID (I find it too limiting for me) but you should probably consider it. Note that XML Schema tools will not by default generate unique IDs for you (although XSLT will).
Note that the name of the attribute (in your case “id”) can be any valid name (e.g. “Id”, “foo”) but it must be declared in the DTD or Schema as of type ID.
UPDATE: OP asks why we don’t use XML ID. We frequently merge nodes from different sources , e.g.
and
The IDs are whatver the authors have chosen (this is not a single database app – think more of people creating blogposts in a community and creating their ids. So it’s common for people to create the same ID (mainly m1). In that case we might well have:
This looks horrid but there are no easy answers.
We use other information to disambiguate. We have a rule that ids are unique within a certain scope – e.g. that the atoms within a molecule must have unique IDs.
If we used a unique ID generator these are completely opaque and the humans in our community would rebel (they did).
IDs are not easy to manage in an unmanaged human community!