xml - 在 XSLT 1.0 中的 for-each 中的谓词中,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 中。我在 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
因为它在谓词中,还是artist
来自for-each
.
解决方案
嗯,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>
:
<xsl:value-of select="/catalog/cd/song[song_artist_number = ./artist_number]/song_title"/>
这是有道理的,因为<artist_number>
可能是的孩子<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 之外也不可用。
推荐阅读
- javascript - 单击时导航栏未更新为活动状态
- ios - 带有内容插入的滚动视图的语音辅助功能分页问题
- java - 如何转发 HTTP 响应
- mongodb - MongoDB聚合 - 聚合管道内的距离排序
- vim - 有没有办法获得可视块选择的最右侧列?
- conv-neural-network - 使用自定义 obs 形状时如何指定 conv_filters?
- node.js - Windows 中 npx create-react-app 中的 npm start 错误
- javascript - 如何在 Maven 动态 Web 项目中从 JavaScript 中的属性文件获取数据
- javascript - 如何在嵌套数组javascript中过滤掉具有不同键的对象
- filter - 谷歌表格的条件格式在另一个表格的列表中