There is a standard XPath function to reference elements by their "id" attribute.
From the XPath 1.0 spec.:
The id()
function function selects elements by their unique ID
(see [5.2.1 Unique IDs]). When the argument to id
is of type node-set
, then the result is the union of the result of applying id
to the string-value
of each of the nodes in the argument node-set
. When the argument to id
is of any other type, the argument is converted to a string
as if by a call to the string function
; the string
is split into a whitespace-separated list of tokens
(whitespace
is any sequence of characters matching the production S
); the result is a node-set
containing the elements in the same document
as the context node
that have a unique ID
equal to any of the tokens in the list.
Another, more generic way of referring to nodes (not only elements) is possible in XSLT. The <xsl:key/>
instruction and the XSLT key()
function are specifically designed for this purpose.
For example, suppose a document contains bibliographic references in the form XSLT, and there is a separate XML document bib.xml containing a bibliographic database with entries in the form:
<entry name="XSLT">...</entry>
Then the stylesheet could use the following to transform the bibref elements:
<xsl:key name="bib" match="entry" use="@name"/>
<xsl:template match="bibref">
<xsl:variable name="name" select="."/>
<xsl:for-each select="document('bib.xml')">
<xsl:apply-templates select="key('bib',$name)"/>
</xsl:for-each>
</xsl:template>
Do note, that keys in XSLT overcome the following limitations of the id()
function:
ID attributes must be declared as
such in the DTD. If an ID attribute
is declared as an ID attribute only
in the external DTD subset, then it
will be recognized as an ID attribute
only if the XML processor reads the
external DTD subset. However, XML
does not require XML processors to
read the external DTD, and they may
well choose not to do so, especially
if the document is declared
standalone="yes"
.
A document can contain only a single
set of unique IDs. There cannot be
separate independent sets of unique
IDs.
The ID of an element can only be
specified in an attribute; it cannot
be specified by the content of the
element, or by a child element.
An ID is constrained to be an XML
name. For example, it cannot contain
spaces.
An element can have at most one ID.
At most one element can have a
particular ID.
Because of these limitations XML documents sometimes contain a cross-reference structure that is not explicitly declared by ID/IDREF/IDREFS attributes.