返回当前节点作为唯一成员的节点集。
node-set current()
备注
该函数返回当前节点作为唯一成员的节点集。 对于最外面的表达式,即没有出现在其他表达式中的表达式,当前节点总是与上下文节点相同。 因此,
<xsl:value-of select="current()"/>
等同于
<xsl:value-of select="."/>
但是,在方括号中,当前节点通常与上下文节点不同。 例如,
<xsl:apply-templates select="//glossary/item[@name=current()/@ref]"/>
处理所有具有 <glossary> 父元素并且 name 属性值等于当前节点的 ref 属性值的 <item> 元素。 这不同于
<xsl:apply-templates select="//glossary/item[@name=./@ref]"/>
,后者表示等同于
<xsl:apply-templates select="//glossary/item[@name=@ref]"/>
所以,将处理所有具有 <glossary> 父元素并且 name 特性和 ref 特性的值相同的 <item> 元素。
示例
XML 文件 (current.xml)
<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="current.xsl" ?>
<nodes>
<node>first</node>
<node>1</node>
<node>
<obj>class</obj>
</node>
</nodes>
XSLT 文件 (current.xsl)
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="current()"/>
</xsl:template>
<xsl:template match="*">
<blockquote><xsl:apply-templates/></blockquote>
</xsl:template>
</xsl:stylesheet>
输出
以下是显示在浏览器中的格式化输出。
first
1
class
以下是 XSLT 处理器的输出。 若要获得此输出,请右键单击浏览器并选择“查看 XSL 输出”菜单项。
<?xml version="1.0" encoding="UTF-16"?>
<blockquote>
<blockquote>first</blockquote>
<blockquote>1</blockquote>
<blockquote>
<blockquote>class</blockquote>
</blockquote>
</blockquote>