現在のノードを唯一のメンバとして持つノード セットを返します。
node-set current()
解説
この関数は、現在のノードのみをメンバとして持つノード セットを返します。最も外側の式、つまり、他の式の中に出現しない式の場合、現在のノードは常にコンテキスト ノードと同じになります。したがって
<xsl:value-of select="current()"/>
上の例と下の例は同じものです。
<xsl:value-of select="."/>
ただし、角かっこ内では、通常、現在のノードはコンテキスト ノードと異なります。次に例を示します。
<xsl:apply-templates select="//glossary/item[@name=current()/@ref]"/>
上の例では、<glossary>
を親要素として持ち、現在のノードの ref
属性と値が同じ name
属性を持つすべての <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>