按照唯一 ID 选择元素。
node-set id(object)
备注
如果参数属于节点集类型,结果是将 id() 应用于 node-set 参数中每个节点的字符串值的结果的联合。
如果参数属于任何其他类型,将转换为字符串,然后拆分成通过空白分隔的标记列表(空白是与输出匹配的任何字符序列);结果是包含与上下文节点处于同一文档中的元素的节点集,这些元素的唯一 ID 等于列表中的任意标记。
示例
XML 文件 (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 文件 (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>
输出
应用于上述 XML 文件的 XSLT 样式表将产生下列节点集:
<x a="a21">
<x a="a31">
<y>y31</y>
<y>y32</y>
</x>
</x>
and
<y>y31</y>