Finding an Element with a Particular ID

To obtain the element that has a particular identifier (ID), the DTD must specify an attribute for that element. The type of this attribute must be ID. The name of the attribute is not significant, though it is typically id. If there is such an attribute, you can call the id() function to obtain the element with a particular ID. The format is

node-set  id(object)  

The id() function evaluates to a set. It ignores the context node set except to evaluate the function's argument. The result set contains an element node that has an attribute of type ID whose value is identical to the string the object argument evaluates to. The element node can appear anywhere in the document that is being queried.

For example:

id("special") 

This query searches for an element that has an attribute whose

Details about working with IDs are in the following topics:

The id() Function's Argument

When the id() function's argument is of type node-set, the result is the union of the results of applying id() to the string value of each of the nodes in the argument node set.

When the argument of id() is any other type, the XPath processor converts the argument to a string as if by a call to the string() function. The XPath processor splits the string into a white-space-separated list of tokens. The result is a node set that contains the elements in the same document as the context node that have a unique ID equal to any of the tokens in the list.

Unique IDs

An element node can have a unique ID. This is the value of the attribute that is declared in the DTD as type ID. No two elements in a document can have the same unique ID. If an XML processor reports two elements in a document as having the same unique ID (which is possible only if the document is invalid), the second element is treated as not having a unique ID.

If a document does not have a DTD, the id() function always returns an empty node list.

 
Free Stylus Studio XML Training:
W3C Member