Deciding whether to Use IDs or Keys

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Deciding whether to Use IDs or Keys

The XML specification itself provides a simple means to uniquely identify an element, using an ID-type attribute. A conforming XML processor, such as the MSXML processor, is expected to build an index of any elements with IDs, enabling them to be cross-referenced. They might be cross-referenced, for example, by other elements with a corresponding IDREF-type attribute.

Why, then, does the XSLT standard provide keys for essentially the same purpose? There are several drawbacks to using IDs alone:

  • You must use an attribute value as the element's ID. That is, you can't use the element's content as an ID, or the content of some other element related to the element you want to identify. For example, if an author's name is a desired index value, but is held in an <author_name> element rather than as an attribute value, there is no way to index the name using XML alone.
  • A given element can have no more than one ID-type attribute. This means, for example, that you cannot hold both a book's ISBN and its publication date in ID-type attributes.
  • Each ID value must be unique in a given document. If pub_date is an ID-type attribute, this effectively prevents two books from sharing the same publication date.
  • In order to use ID attributes at all, they must be declared as such in a Document Type Definition (DTD). If the document is merely well-formed and therefore does not use a DTD, none of its attributes can be IDs.
  • All IDs in a document constitute a single set of IDs. You can't establish multiple sets of IDs that are different but related. For example, if you've designed a document type that uses ISBNs to uniquely identify books, and author numbers to uniquely identify authors, there's no way to treat these two kinds of IDs distinctly.
  • The name of an XML ID must be a legitimate XML name. For example, it cannot contain spaces, and it must start with a letter. This forces you to use somewhat artificial values, such as "isbn0123456789" and "AFarewellToArms" as the unique identifier, rather than "0123456789" and "A Farewell to Arms," respectively.

The use of keys enables an XSLT style sheet to circumvent any or all of these restrictions.