我正在尝试使用前一个兄弟轴、一个过滤器和一个索引器来检索 for-each 中的前一个节点以找到它。
问题是,我只想要与选择器中的 XPath 匹配的第一项,但是,我似乎无法同时应用过滤器和索引器。索引器似乎覆盖了过滤器,所以我总是得到第一个前面的兄弟而不是第一个匹配过滤器的兄弟。
我已经尝试将它放在 foreach 内的变量中,但由于无法更改变量,因此节点集始终是第二项的前一个兄弟值。
这是相关代码(尽可能多地消除噪音)。为 umbraco 噪音道歉(XSLT 用于为我无法轻易更改的子导航生成一些相当复杂的标记)。
<xsl:for-each select="$currentPage/ancestor-or-self::node[(@nodeTypeAlias='Discover' or @nodeTypeAlias='CampaignHome') and data[@alias='umbracoNaviHide'] != 1]/child::node[(@nodeTypeAlias='Discover' or @nodeTypeAlias='CampaignHome') and data[@alias='umbracoNaviHide'] != 1]">
<!--This variable is always set to the second item's preceding sibling-->
<xsl:variable name="precedingItem" select="preceding-sibling::node[data[@alias='umbracoNaviHide' != 1]]" />
<!--This variable always contains the second item even if /data/@alias='umbracoNaviHide' = 1 -->
<xsl:variable name="predingItemWithIndexer" select="preceding-sibling::node[data[@alias='umbracoNaviHide' != 1]][1]" />
<!-- this always prints out the id of the first item -->
<xsl:value-of select="position()" /> <xsl:value-of select="$precedingItem[1]/@id" />
</xsl:foreach>
我会使用第二个内联选择器,但因为索引器过滤器覆盖了另一个过滤器,所以当 umbracoNaviHide = 1 时它没有给出正确的值。