XSLT 1.0 中 for-each 中的谓词中的 current() 引用什么?
What does current() reference inside a predicate that's inside a for-each in XSLT 1.0?
我经常看到这样的代码:
<xsl:for-each select="/catalog/cd/artist">
<xsl:sort select="artist"/>
...business logic...
<xsl:variable name="artistNum" select="artist_number"/>
<xsl:value-of select="/catalog/cd/song[song_artist_number = $artistNum]/song_title"/>
</xsl:for-each>
变量artistNum
仅在value-of
中使用一次,以确保使用了正确的节点。这些数字在 SQL 中的作用类似于外键,但在 XML 中。我读到 on W3Schools,current()
和 .
在特定情况下的含义确实略有不同。所以我想知道以下是否也是正确的,允许删除几乎无用的变量 artistNum
。
<xsl:for-each select="/catalog/cd/artist">
<xsl:sort select="artist"/>
...business logic...
<xsl:value-of select="/catalog/cd/song[song_artist_number = current()/artist_number]/song_title"/>
</xsl:for-each>
但我不确定那个上下文中的 current()
是指 song
因为它在谓词中,还是指 for-each
中的 artist
。
嗯,current()
指的是当前节点。 :)
<xsl:for-each select="/catalog/cd/artist">
<!-- processes `<artist>` elements - current() always refers to that element --->
</xsl:for-each>
current()
的存在是为了克服 .
指的是 XPath 谓词正在运行的节点,而 XPath(从其有限的世界观来看)没有访问 XSLT 的上下文。
这没有意义,因为 <artist_number>
可能不是 <song>
的 child:
<xsl:value-of select="/catalog/cd/song[song_artist_number = ./artist_number]/song_title"/>
这是有道理的,因为 <artist_number>
可能 是 的 child of <artist>
:
<xsl:for-each select="/catalog/cd/artist">
<xsl:value-of select="/catalog/cd/song[song_artist_number = current()/artist_number]/song_title"/>
</xsl:for-each>
XSLT 中的一些内容更改了 current()
节点 - 最显着的是 <xsl:for-each>
和 <xsl:apply-templates>
(但不是 <xsl:call-template>
)。
从根本上说,.
是一个 XPath 概念。它指的是 XPath 表达式中不同位置的不同节点。 current()
是一个 XSLT 概念。它引用单个节点,直到 XSLT 程序的处理上下文发生变化。它在 XSLT 之外也不可用。
我更喜欢用与@Tomalak 不同的方式来解释它(尽管他的解释并没有错)。
将 current() 视为变量而不是函数。它很容易被命名为 $xsl:current。每当您在样式表中看到 XPath 表达式时,例如 select="XXXXXX"
,然后将其替换为
let $xsl:current := . return XXXXXX
因此,每当执行从 XSLT 切换到 XPath 时,$xsl:current
变量就会隐式绑定到“.”的值;什么时候 ”。”由于 XPath 构造(如谓词)而发生更改,变量 $xsl:current
保留其原始值。
我经常看到这样的代码:
<xsl:for-each select="/catalog/cd/artist">
<xsl:sort select="artist"/>
...business logic...
<xsl:variable name="artistNum" select="artist_number"/>
<xsl:value-of select="/catalog/cd/song[song_artist_number = $artistNum]/song_title"/>
</xsl:for-each>
变量artistNum
仅在value-of
中使用一次,以确保使用了正确的节点。这些数字在 SQL 中的作用类似于外键,但在 XML 中。我读到 on W3Schools,current()
和 .
在特定情况下的含义确实略有不同。所以我想知道以下是否也是正确的,允许删除几乎无用的变量 artistNum
。
<xsl:for-each select="/catalog/cd/artist">
<xsl:sort select="artist"/>
...business logic...
<xsl:value-of select="/catalog/cd/song[song_artist_number = current()/artist_number]/song_title"/>
</xsl:for-each>
但我不确定那个上下文中的 current()
是指 song
因为它在谓词中,还是指 for-each
中的 artist
。
嗯,current()
指的是当前节点。 :)
<xsl:for-each select="/catalog/cd/artist">
<!-- processes `<artist>` elements - current() always refers to that element --->
</xsl:for-each>
current()
的存在是为了克服 .
指的是 XPath 谓词正在运行的节点,而 XPath(从其有限的世界观来看)没有访问 XSLT 的上下文。
这没有意义,因为 <artist_number>
可能不是 <song>
的 child:
<xsl:value-of select="/catalog/cd/song[song_artist_number = ./artist_number]/song_title"/>
这是有道理的,因为 <artist_number>
可能 是 的 child of <artist>
:
<xsl:for-each select="/catalog/cd/artist">
<xsl:value-of select="/catalog/cd/song[song_artist_number = current()/artist_number]/song_title"/>
</xsl:for-each>
XSLT 中的一些内容更改了 current()
节点 - 最显着的是 <xsl:for-each>
和 <xsl:apply-templates>
(但不是 <xsl:call-template>
)。
从根本上说,.
是一个 XPath 概念。它指的是 XPath 表达式中不同位置的不同节点。 current()
是一个 XSLT 概念。它引用单个节点,直到 XSLT 程序的处理上下文发生变化。它在 XSLT 之外也不可用。
我更喜欢用与@Tomalak 不同的方式来解释它(尽管他的解释并没有错)。
将 current() 视为变量而不是函数。它很容易被命名为 $xsl:current。每当您在样式表中看到 XPath 表达式时,例如 select="XXXXXX"
,然后将其替换为
let $xsl:current := . return XXXXXX
因此,每当执行从 XSLT 切换到 XPath 时,$xsl:current
变量就会隐式绑定到“.”的值;什么时候 ”。”由于 XPath 构造(如谓词)而发生更改,变量 $xsl:current
保留其原始值。