XSLT获取父节点未键入的所有节点

时间:2016-07-25 12:19:07

标签: xslt umbraco

我的结构类似如下:

<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")]

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

1 个答案:

答案 0 :(得分:0)

您可以使用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模板是如何形成的,以及这是否真的可以发生。

相关问题