SonarQube如何使用xpath为xml创建自定义规则

时间:2017-07-20 03:20:18

标签: xml xpath sonarqube

  • SonaQube:6.4
  • 声纳扫描仪:3.0.3
  • 项目名称:testxml

我已将指南https://docs.sonarqube.org/display/DEV/Adding+Coding+Rules+using+XPath置于创建xpath规则,并在saas xml rules中激活。

xpath规则7文件如下:

  • 名称 - > myXmlRule
  • keyword - > myXmlRule
  • description - >测试
  • 严重性 - >主要
  • 雕像 - >就绪
  • filePattern - >
  • 架构 - > // ATTRIBUTE [@tokenValue =' lang']

//ATTRIBUTE[@tokenValue='lang']中的模式xmlToolKit成功,这意味着模式是正确的。 enter image description here

名为myXmlRule的新规则已在Qualiy Profile saas xml rules

中生效

enter image description here enter image description here

我的目标是找到具有title属性的lang节点,然后将其提升。 怎么写规则?

test.xml文件内容:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>
<book>
    <title lang="eng">Harry Potter</title>
    <price>29.99</price>
</book>
<book>
    <title lang="eng">Learning XML</title>
    <price>39.95</price>
</book>
</bookstore>

在我的项目testxml中仅test.xmlsonar-project.properties

在项目文件夹中运行sonar-scanner,它会收到错误消息

WARN: SCM provider autodetection failed. No SCM provider claims to support this project. Please use sonar.scm.provider to define SCM of your project.
16:41:11.456 WARN: Could not find schema //ATTRIBUTE[@tokenValue='lang']
16:41:11.456 WARN: Cannot validate file /Users/zt/Desktop/testXml/test.xml
16:41:11.457 WARN: Cause: org.sonar.plugins.xml.checks.XmlSchemaCheck$SchemaNotFoundException: Could not load schema "//ATTRIBUTE[@tokenValue='lang']"

请帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

创建XPath检查的规则是&#39; 跟踪违反XPath规则的行为&#39; ( xml:XPathCheck ),而不是&#39; XML文件应该有效&#39; (截图中显示的那个)。