伟大的祖先&伟大的祖先

时间:2016-02-02 15:36:51

标签: xpath

1 /我有规则检查器,根据xpath表达式禁止元素。

2 /每个"测试"元素可以包含" test"元素递归

3 /我想禁止"非使用"第一,第三,第三次尝试#34;元素

例:

<test targetAttribute="level 1">
   <test targetAttribute="level 2">
      <test targetAttribute="level 3">
         <test targetAttribute="level 4">
            <test targetAttribute="level 5">
            </test>
         </test>
      </test>
   </test>
</test>

targetAttribute属性对于第一级3级是必需的,只有级别3的所有其他后代元素都具有其targetAttribute选项。

这是我的xpath:

//test[not(targetAttribute)]/ancestor[1]::test (level1)
//test[not(targetAttribute)]/ancestor[2]::test (level2)
//test[not(targetAttribute)]/ancestor[3]::test (level3)

但它不起作用!我也尝试过没有成功:

//test/ancestor[1]::test[not(targetAttribute)]

我变得疯狂@ _ @有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

要选择属性,您需要在属性名称前使用@。所以

//test[not(targetAttribute)]

应改为

//test[not(@targetAttribute)]

它将获取所有不包含此test的{​​{1}}元素。

第二件事。 当你想选择第一个最接近的祖先@targetAttribute时,你应该在测试后使用索引,如下所示:

test

这将选择正在测试的关闭祖先(在这种情况下是直接父级)。

/ancestor::test[1]

会给你祖父母,而/ancestor::test[2] 会产生盛大的父母。

此外,您应该过滤掉没有3

的祖先

不确定您要完成的是什么,只是尝试一下:

@targetAttribute(等级1)

//test[not(@targetAttribute)]/ancestor::test[@targetAttribute][3](第2级)

//test[not(@targetAttribute)]/ancestor::test[@targetAttribute][2](第3级)