节点集上的 XSLT 过滤和索引
我试图使用前同级轴、过滤器和索引器来检索 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 时,它不会给出正确的值。
I'm trying to retreive the previous node in a for-each using the preceding-sibling axis, a filter and an indexer to find it.
The problem is, I only want the first item that matches the XPath in the selector however, I seem unable to apply both the filter and the indexer. The indexer seems to override the filter so I always get the first preceding sibling rather than the first one matching the filter.
I've tried putting this in a variable inside the foreach but since the variable cannot be changed the nodeset is always the second item's preceding sibling's values.
Here's the relevant code (chopped out as much noise as possible). Apologies for the umbraco noise (the XSLT is for generating some fairly complicated markup for a subnav which I can't change easily).
<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>
I would use the second selector inline but because the indexer filter is overriding the other filter it doesn't give the correct values when umbracoNaviHide = 1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这意味着:选择至少有一个不带
alias
属性或带子
属性不等于“umbracoNaviHide”data
元素的所有前述同级node
元素>alias编辑:这是正确的 XPath:
This means: select all the preceding sibling
node
elements having at least one childdata
element without analias
attribute or with analias
attribute not equal to "umbracoNaviHide"EDIT: This is the correct XPath for this: