id FunctionÂ
Selects elements by their unique ID.
node-set id(object)
Remarks
When the argument is of type node-set, the result is the union of the result of applying id()
to the string value of each of the nodes in the node-set argument.
When the argument is of any other type, it is converted to a string and then split into a white space–separated list of tokens (white space is any sequence of characters matching the production); 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.
Example
XML FILE (test.xml)
<?xml version="1.0"?>
<!DOCTYPE test [
<!ELEMENT test (x+)>
<!ELEMENT x (x+| y+)>
<!ATTLIST x
a ID #REQUIRED>
<!ELEMENT y ANY>
]>
<test>
<x a="a11">
<x a="a21">
<x a="a31">
<y>y31</y>
<y>y32</y>
</x>
</x>
</x>
<x a="a12">
<x a="a22">
<y>y21</y>
<y>y22</y>
</x>
</x>
<x a="a13">
<y>y11</y>
<y>y12</y>
</x>
<x a="a14">
<y>y03</y>
<y>y04</y>
</x>
</test>
XSLT File (test.xsl)
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<!-- suppress text nodes not covered in subsequent template rule -->
<xsl:template match="text()"/>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:apply-templates select="*|@*"/>
<xsl:if test="text()">
<xsl:value-of select="."/>
</xsl:if>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="/test">
<xsl:apply-templates select="id('a21') "/>
and
<xsl:apply-templates select="id('a11')//y[1]"/>
</xsl:template>
</xsl:stylesheet>
Output
The XSLT stylesheet, when applied to the XML file above results in the following node-set:
<x a="a21">
<x a="a31">
<y>y31</y>
<y>y32</y>
</x>
</x>
and
<y>y31</y>