首页 > 解决方案 > Sitecore Xpath 比较两个字段

问题描述

我想要一个查询,查看其父级的名称,然后导航到内容文件夹下具有相同站点名称的文件夹,并在多列表中显示其下的项目。

基本上内容树中的结构如下所示:

当我想开始将相对父级的“名称”与绝对路径的子级的“名称”进行比较时,麻烦就开始了。在 Xpath 中,它可能会像下面描述的那样: 使用 Xpath 比较属性值

在这种情况下,到目前为止我有以下查询:

/sitecore/content/*[ancestor-or-self::*[@@templateid='{Template Id of Sitename}']=@@name and @@templateid='{Template Id of Sitename}']

此查询返回 Sitename1 和 Sitename2。

有趣的是,如果我替换“ancestor-or-se..”或“@@name”部分 bij“Sitename1”,如下所示:

/sitecore/content/*['Sitename1'=@@name and @@templateid='{Template Id of Sitename}']

..和..

/sitecore/content/*[./ancestor-or-self::*[@@templateid='{Template Id of Sitename}']='DSW' and @@templateid='{Template Id of Sitename}']

我得到了想要的结果:Sitename1。

顺便说一句,在将查询粘贴到“带搜索的多列表”之前,我现在使用的是内置的 xpath 构建器。

任何帮助,将不胜感激。

编辑:
我想我发现当我开始相对查询(“./ancestor::...”部分)时,它实际上是相对于我最终得到绝对查询的项目。所以我应该有以下查询:

./ancestor-or-self::*[@@templateid='{Template Id of Sitename}' and @@name=ancestor::*[@@templateid='{Template Id of root item aka "sitecore"}']//*[@@templateid={Template Id of Sitename}]]  

在这里,我收到错误“对象必须是字符串类型”,这可能是由于上一个查询的以下部分:

@@name=ancestor::*[@@templateid='{Template Id of root item aka "sitecore"}']//*[@@templateid={Template Id of Sitename}]  

正确的部分不能解决字符串。所以问题仍然存在,如何使用 sitecore xpath 从 sitecore 项目中提取纯字符串以便能够进行比较。

标签: asp.netxpathsitecoresitecore8

解决方案


我发现 Sitecore 至少不支持快速查询的子查询,我认为这同样适用于普通查询(另请参见:此处的“不支持子查询” )。现在让我使用简单的代码来执行两个查询。一种非常简单的方法是从 IDatasource 继承(在 sitecore.buckets.dll 中),您需要编写“code:{fullpath to class}, assemblyname.dll”(另见:here


推荐阅读