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

  • Type is ID
  • Value is special

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.

XSL-List Blog

XSL-List is a discussion forum on the Extensible Stylesheet Language (XSL), covering the XSL specification, XSL processors, tools, user questions, trends and more. Bookmark this RSS or ATOM feed now!

Web Service Data-to-XML Mapper

Stylus Studio provides a powerful standards-based solution for accessing and transforming live Web service XML data. Intuitive tools like the Web Service Call Composer and UDDI Browser make developing Web service applications a snap.

XML Editor

Stylus Studio XML editor includes many XML editing utilities and features to simplify XML development, including intuitive XML editing views, integrated XML validation and troubleshooting utilities, XML formatting tools, XML generation tools, etc.

Apache XML Project

The Apache XML Project provides standards-based, open-source XML solutions for working with W3C XML technologies including XML, XSL, XSLT, XSL:FO XPath, Web services and more. Learn how Stylus Studio provides support for all Apache XML projects.

Stylus Most Wanted

 
Free Stylus Studio XML Training:
W3C Member