节点集上的 XSLT 过滤和索引

发布于 2024-09-13 03:56:49 字数 1305 浏览 8 评论 0原文

我试图使用前同级轴、过滤器和索引器来检索 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栩栩如生 2024-09-20 03:56:49
preceding-sibling::node[data[@alias='umbracoNaviHide' != 1]]

这意味着:选择至少有一个不带 alias 属性或带 data 元素的所有前述同级 node 元素>alias 属性不等于“umbracoNaviHide”

编辑:这是正确的 XPath:

我想要第一个前面的节点
包含数据元素的元素
有一个名为的属性别名
umbracoNaviHide 其中不包含
值 1。

preceding-sibling::node[data[@alias='umbracoNaviHide']!=1][1]
preceding-sibling::node[data[@alias='umbracoNaviHide' != 1]]

This means: select all the preceding sibling node elements having at least one child data element without an alias attribute or with an alias attribute not equal to "umbracoNaviHide"

EDIT: This is the correct XPath for this:

I wanted the first preceding node
element which contains a data element
that has an attribute alias called
umbracoNaviHide which does not contain
the value 1.

preceding-sibling::node[data[@alias='umbracoNaviHide']!=1][1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文