当从任何深度的类似“contextItem”元素执行时,如何编写相对 Sitecore 查询(XPath 的松散版本)查询以仅返回下面的元素“目标”(即第三深祖先)?由于我正在执行查询的上下文,它必须相对于“contextItem”(或任何元素将成为查询的上下文)。
此外,我不知道我需要选择的项目的名称,只知道级别和它是一个祖先(以及它总是出现在 /sitecore/content 下)。
执行上下文是 Sitecore 7.2,查询将用于选择紧接在 /sitecore/content 下的祖先。谢谢你。
<sitecore>
<content>
<target>
<level>
<level>
<contextItem>
</contextItem>
</level>
</level>
</target>
<target2>
<otherStuff/>
</target2>
</content>
<system>
<stuff/>
</system>
<templates>
<otherStuff/>
</templates>
</sitecore>
以下 Sitecore 查询成功选择了我想要的项目,但也选择了它与上下文项目之间的所有干预祖先:
./ancestor::*[ancestor::*[@@key='content']]
这些似乎工作,但不灵活和hacky:
./ancestor::*[@@key='home']/ancestor::*[@@key != 'sitecore' and @@key != 'content']
./ancestor::*[position() = 1 and @@key != 'sitecore' and @@key != 'content']
编辑:这些也有效:
./ancestor::*[ancestor::*[@@key='content']]/.[1]
./ancestor::*[ancestor::*[parent::content]]/.[1]