1/我有规则检查器,它禁止依赖于xpath表达式的元素。
2/每个"test“元素都可以递归地包含"test”元素。
我想禁止第三次“测试”元素的“不使用”态度。
例:
<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属性对于第三个级别是强制性的,只有来自level3的所有其他后代元素都有自己的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)]
我快疯了,@_@,有人能帮我吗?
发布于 2016-02-02 16:55:11
为了选择属性,您需要在属性名称之前使用@
。所以
//test[not(targetAttribute)]
应改为
//test[not(@targetAttribute)]
它将获得不包含此test
的所有@targetAttribute
元素。
第二件事。当您想要选择第一个最近的祖先test
时,应该在测试之后使用索引,如下所示:
/ancestor::test[1]
这将选择测试的关闭祖先(在本例中为直系亲属)。
/ancestor::test[2]
会给你祖父母,3
会生下祖父母。
另外,您可能应该过滤掉没有@targetAttribute
的祖先
不知道你到底想要完成什么,但只要试一试:
//test[not(@targetAttribute)]/ancestor::test[@targetAttribute][3]
(level1)
//test[not(@targetAttribute)]/ancestor::test[@targetAttribute][2]
(level2)
//test[not(@targetAttribute)]/ancestor::test[@targetAttribute][1]
(level3)
https://stackoverflow.com/questions/35157603
复制相似问题