How to have nested conditions for PMD Xpath rules

天大地大妈咪最大 提交于 2019-12-01 10:54:03

问题


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.

Piece 1:

 //PrimaryExpression[not(PrimarySuffix/Arguments)]

Piece 2:

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

Piece 3:

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

回答1:


You need to have at least some basic knowledge/understanding of 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.

For example:

/*/num

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

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]

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.

. 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 ...

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().

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'
   ] 


来源:https://stackoverflow.com/questions/8998402/how-to-have-nested-conditions-for-pmd-xpath-rules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!