Custom XPATH rule is not working

I am also facing same kind of issue, I am trying to validate if a file name weblogic.xml, If the file doesn’t have declared the attribute names as page-check-seconds then report violation.

filePattern : weblogic.xml
The XPath query = not(//jsp-descriptor/jsp-param/param-name[text()=‘page-check-seconds’])
Message : Page check seconds are not mentioned

File content:

<?xml version="1.0" encoding="UTF-8"?> keepgenerated true page-check-sec true

But I am not able to see any sonar violation , I an scanning file using sonar-scanner. I am using soanrqube 7.2.

PS: I am novice to SonarQube

Hello,

In the reference documentation you have this section about XPath rule:

For XML, which is already immediately accessible to XPath, you can simply write your rules and check them using any of the freely available tools for examining XPath on XML.

Did you try these freely available tools to validate your XPath query before trying to use it in SQ?

Thanks

Hi Alex,

Yes I Did, at https://www.webtoolkitonline.com/xml-xpath-tester.html

XPATH : not(//jsp-descriptors/jsp-param/param-name[text()=‘page-check-seconds’])

XML: <?xml version="1.0" encoding="UTF-8"?>page-check-sec1

Hello,

Can you copy/paste here an example of weblogic.xml file containing the “page-check-seconds” attribute?

Just a suggestion to discard this problem: check the single quotes surrounding page-check-seconds, they look weird. I had on my side to replace them with real single quote: ‘page-check-seconds’

Uploaded weblogic.xml

Modified XPATH is :

concat(count(//jsp-descriptor/jsp-param/param-name[contains(text(),‘page-check-seconds’)])>0,substring(false, 1 div not(//jsp-descriptor/jsp-param/param-name[contains(text(),‘page-check-seconds’)])))

but then also it is not working, any help will be really great. Thanks !!

weblogic.txt (218 Bytes)

To raise an XPath issue on a file, the XPath expression has to return a boolean. As your last expression is the result of the concat function, it returns a string.

I suggest using: count(//jsp-descriptor/jsp-param/param-name[text()='page-check-seconds']) = 0

One more thing: if the pattern of the XPath rule is weblogic.xml, it will only look for a file named weblogic.xml at the root of the project. You may want to set the pattern to **/weblogic.xml to scan all files named weblogic.xml.