在 XSL 中使用 for-each 时如何访问当前项目的值?

问题描述 投票:0回答:1

使用下面的代码,如何在不指定 $array[1] 的情况下访问循环中的当前项?

    <xsl:variable name="array" as="element()*">
        <item>https://www.example.com/_resources/data/blogs.xml</item> <!--item 1-->
        <item>https://www.example.com/_resources/data/blogs2.xml</item> <!--item 2-->
    </xsl:variable>
    
    <xsl:for-each select="$array/item">
        <xsl:apply-templates select="document($array[1])/hero-image/img" /><!--How do we access value of current item without specifying $array[1]) -->
    </xsl:for-each>
xml xslt xslt-2.0
1个回答
0
投票

首先,变量

$array
保存一系列
item
元素,并且您想要迭代这些元素,而不是它们的子元素。您编写了
$array/item
,它是
$array/child::item
的缩写,它选择数组中元素的子元素,而不是元素本身。

其次,您将当前项目称为

.
。所以你会想要:

<xsl:for-each select="$array">
    <xsl:apply-templates select="document(.)/hero-image/img"/>
</xsl:for-each>

但这可以缩写为

<xsl:apply-templates select="$array/document(.)/hero-image/img"/>

注意:您在这里使用的是 XSLT 2.0+,最好在问题中提及(或者简单地添加相关标签,我将为您做)。

© www.soinside.com 2019 - 2024. All rights reserved.