There is a standard XPath function for referencing elements by the id attribute .
From XPath 1.0 spec .:
The id() function function selects elements by their unique ID (see [5.2.1 Unique identifiers]). If the ID argument is of type node-set , then the result is the union of the result of applying the ID to the string-value each of the nodes in the node-set argument. When the ID argument is of any other type, the argument is converted to string , as if calling a string function ; string divided into a whitespace-separated list of tokens ( whitespace is any sequence of characters corresponding to the product S ); the result is a node-set containing elements in the same document as context node that have a unique ID equal to any of the tokens in the list.
Another, more general way to access nodes (not just elements) is possible in XSLT . The <xsl:key/> command and XSLT key() specifically designed for this purpose.
For example, suppose a document contains bibliographic references in XSLT form, and there is a separate bib.xml XML document containing a bibliographic database with entries in the form:
<entry name="XSLT">...</entry>
Then in the stylesheet, you can 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>
Note that keys in XSLT overcome the following limitations of the id() function:
Attribute identifiers must be declared as such as DTDs. If an identifier attribute is declared only as an identifier attribute in an external DTD subset, then this will be recognized as an identifier attribute only if the XML processor reads the external DTD set. However, XML does not require XML processors to read an external DTD, and they may not select this, especially if the document is declared standalone="yes" .
A document can contain only one set of unique identifiers. There cannot be separate independent sets of unique Identifiers.
The identifier of the element can only be specified in the attribute; it cannot be indicated in the content of an element or child.
The identifier is limited to the XML interface name. For example, it cannot contain space.
An element can have no more than one identifier.
No more than one element can have a specific identifier.
Due to these limitations, XML documents sometimes contain a cross-reference structure that is not explicitly declared by the ID / IDREF / IDREFS attributes.
source share