且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为 PMD Xpath 规则设置嵌套条件

更新时间:2022-12-10 11:04:36

您至少需要对 XPath 有一些基本的知识/理解.

我看到在一些 [] 里面的语句的开头使用了 .示例代码.它们有什么用?

I saw the use of . in the beginning of statement inside [] in some example code. what are they used for?

[] 被称为谓词.它必须包含一个布尔表达式.它必须立即遵循节点测试.这为满足要选择的节点测试的节点指定了附加条件.

[] is called predicate. It must contain a boolean expression. It must immediately follow a node-test. This specifies an additional condition for a node that satisfies the node-test to be selected.

例如:

/*/num

选择所有名为 num 的元素,它们是 XML 文档顶部元素的子元素.

selects all elements named num that are children of the top element of the XML document.

然而,如果我们只想选择这样的 num 元素,其值为奇数,我们在谓词中添加这个附加条件:

However, if we want to select only such num elements, whose value is an odd integer, we add this additional condition inside a predicate:

/*/num[. mod 2 = 1]

现在最后一个表达式选择所有名为 num 的元素,它们是 XML 文档顶部元素的子元素并且其字符串值代表一个奇数.

Now this last expression selects all elements named num that are children of the top element of the XML document and whose string value represents an odd integer.

. 表示上下文节点——这是迄今为止选择的节点(或计算完整 XPath 表达式的起始节点).

. denotes the context node -- this is the node that has been selected so-far (or the starting node off which the complete XPath expression is evaluated).

在我的特殊情况下,我需要将以下部分组合在一起......

In my particular case, I need to combine following pieces together ...

你忘了说这三个表达式应该以何种方式/如何组合.在 XPath 中,一些经常使用的组合符"是运算符 andor 和函数 not().

You forgot to say in what way / how the three expressions should be combined. In XPath some of the frequently used "combinators" are the operators and, or, and the function not().

例如,如果要选择所有三个提供的 XPath 表达式选择的元素,则可以使用 and 运算符:

For example, if you want to select elements that are selected by all three provided XPath expressions, you can use the and operator:

//PrimaryExpression
   [not(PrimarySuffix/Arguments)
  and
    PrimaryPrefix/@Label='this'
   ]