XSLT current() 函數
定義和用法
current() 函數返回僅包含當前節點的節點集。通常,當前節點與上下文節點是相同的。
<xsl:value-of select="current()"/>
等于
<xsl:value-of select="."/>
不過,有一點不同。讓我們看一下下面的 XPath 表達式:"catalog/cd"。表達式選擇了當前節點的 <catalog> 子節點,然后選擇了 <catalog> 節點的 <cd> 子節點。這意味著,在計算的每一步上,"." 都有不同的意義。
下面這行:
<xsl:apply-templates select="//cd[@title=current()/@ref]"/>
將處理 title 屬性的值等于當前節點的 ref 屬性的值的所有 cd 元素。
與這個不同:
<xsl:apply-templates select="//cd[@title=./@ref]"/>
這個會處理 title 屬性和 ref 屬性具有相同值的所有 cd 元素。
語法
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>
查看 XML 文件,查看 XSL 文件,查看結果。