XSLT 获取父节点不是类型的所有节点

XSLT Get All Nodes where parent node is not type

我有一个类似如下的结构:

<list>
   <newsItem title="Item1"></newsItem>
   <newsItem title="Item2"></newsItem>
   <newsItem title="Item3"></newsItem>
   <newsItem title="Item4"></newsItem>
   <newsItemCategory title="Cat1">
          <newsItem title="Item1"></newsItem>
          <newsItem title="Item2"></newsItem>
          <newsItem title="Item3"></newsItem>
          <newsItem title="Item4"></newsItem>
   </newsItemCategory>
   <newsItem title="Item5"></newsItem>
   <newsItem title="Item6"></newsItem>
</list>

我正在做一个 for-each 循环,我想要 newsItem 或 newsItemCateogory 类型的所有节点,但我不想带回 newsItemCategory 中的 newsItems。

要获取我的 XSLT 正在执行的所有操作,请执行以下操作:

$currentPage/ancestor-or-self::root//* [name() = "newsItem" or name() = "newsItemCategory"]

我正在尝试过滤这个:

  $currentPage/ancestor-or-self::root//* [parent::node/name() != "newsItemCategory" and (name() = "newsItem" or name() = "newsItemCategory")]

我不知道这是否是正确的语法或者它是否可能,但它没有带回我正在寻找的结果。

您可以使用 parent::self:: 轴,而无需求助于 name() 检查。

$currentPage/ancestor-or-self::*[last()]//*[
    self::newsItem or self::newsItemCategory
]

我建议使用 not() 而不是 !=:

$currentPage/ancestor-or-self::*[last()]//*[
    (self:newsItem or self::newsItemCategory) and not(parent::newsItemCategory)
]

我使用 ancestor-or-self::*[last()] 以一种通用的方式到达最顶层的元素,因为 ancestor-or-self::root 可能会选择多个元素。我不知道 Umbraco 模板是什么样子的,也不知道这是否真的会发生。