如何为 PMD Xpath 规则设置嵌套条件 [英] How to have nested conditions for PMD Xpath rules

查看:39
本文介绍了如何为 PMD Xpath 规则设置嵌套条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的规则要求我只将它们应用于名称中不包含get"的方法.换句话说,我的规则只需要应用于类中的非 getter 方法.我知道要掌握所有非 getter 方法,我可以使用

My rule requires me to apply them only to methods without 'get' as part of their name. In another words, my rules need to apply to only non-getter methods in the class. I know to get a hold of all the non-getter methods, I can use

//MethodDeclarator[not(contains(@Image,'get'))]

但是,我不知道在何处插入规则逻辑的语法.是不是像

However, I don't know the syntax about where I insert my logic for the rules. Is it like

//MethodDeclarator[
not(contains(@Image,'get'))

 'Some Rule Statements' 
]

我看到了 .在某些示例代码中 [] 内的语句的开头.它们是做什么用的?

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

在我的特殊情况下,我需要将以下部分组合在一起,但到目前为止我还无法完成.

In my particular case, I need to combine following pieces together but so far I am unable to accomplish it yet.

第 1 部分:

 //PrimaryExpression[not(PrimarySuffix/Arguments)]

第 2 部分:

 //MethodDeclarator[not(contains(@Image,'get'))]

第 3 部分:

 //PrimaryExpression[PrimaryPrefix/@Label='this']

推荐答案

您至少需要对 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'
   ] 

这篇关于如何为 PMD Xpath 规则设置嵌套条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆