XSLT current() 函數(shù)
定義和用法
current() 函數(shù)返回僅包含當(dāng)前節(jié)點(diǎn)的節(jié)點(diǎn)集。通常,當(dāng)前節(jié)點(diǎn)與上下文節(jié)點(diǎn)是相同的。
<xsl:value-of select="current()"/>
等于
<xsl:value-of select="."/>
不過(guò),有一點(diǎn)不同。讓我們看一下下面的 XPath 表達(dá)式:"catalog/cd"。表達(dá)式選擇了當(dāng)前節(jié)點(diǎn)的 <catalog> 子節(jié)點(diǎn),然后選擇了 <catalog> 節(jié)點(diǎn)的 <cd> 子節(jié)點(diǎn)。這意味著,在計(jì)算的每一步上,"." 都有不同的意義。
下面這行:
<xsl:apply-templates select="//cd[@title=current()/@ref]"/>
將處理 title 屬性的值等于當(dāng)前節(jié)點(diǎn)的 ref 屬性的值的所有 cd 元素。
與這個(gè)不同:
<xsl:apply-templates select="//cd[@title=./@ref]"/>
這個(gè)會(huì)處理 title 屬性和 ref 屬性具有相同值的所有 cd 元素。
語(yǔ)法
node-set current()
例子
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="catalog/cd/artist">
Current node: <xsl:value-of select="current()
"/>
<br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>