Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Install the XML plugin through the Update Center or download it into the SONAR_HOME/extensions/plugins directory
  2. Restart the Sonar server

Usage

You can analyze your project with the different analyzers.

Sonar Runner

...

Run a Sonar Analysis with the Sonar Runner (Recommended Way)

To launch a Sonar analysis of your XML project, use the Sonar Runner.

A sample project is available on github that can be browsed or downloaded:

...

 /projects/languages/xml/xml-sonar-runner.

Maven

Create a maven pom for your project. Set the following properties:

  • sonar.language: xml
  • sonar.xml.sourceDirectory: [folder of the xml files]
  • sonar.dynamicAnalysis: false

...

Run a Sonar Analysis with the other Analyzers

Maven and Ant can also be used to launch analysis on Web projects.

Advanced Properties

The following properties of the plugin are configurable:

property

description

default value

sonar.xml.sourceDirectory

folder that will be scanned (relative to project root)

 

sonar.xml.fileExtensions

file extensions that will be scanned

xml, xhtml

sonar.xml.includeFileFilter

additional filter for the files to be scanned

 

sonar.xml.schemas

namespace of schema to use for validation

 

The properties may be set in sonar, the maven pom or the maven commmandline.

In the rules profile the main checks are XMLSchemaCheck and XPathCheck. These checks are explained below. 

Schema Validation

XmlSchemaCheck

The rules profile contains the check XmlSchemaCheck. This check can be copied and instantiated multiple times. 

...

Note that the project setting sonar.xml.schemas is a shortcut for configuring the property schemas on the check itself. The project setting will override the setting in the XMLSchemaCheck itself.

Schemas

Schema validation is performed by the xerces parser using XML schemas (XSDs). The plugin has a couple of schema's available:

...

  • You can use the names in the first column to identify the schema for validation, e.g. sonar.xml.schemas=xhtml1-transitional.
  • For validation of XHTML files it is recommended to use autodetect, xhtml1-transitional or xhtml1-strict. 
  • If you specify autodetect the plugin will try to detect the schema from the document itself. 
  • There is no XML schema for HTML 5 provided by W3C.
  • If you need to check with another schema, you can provide the path to the schema. The plugin will search for the schema on filesystem using a relative path. 

Rules Profile

There are quite a few checks related to XML validation. Each check corresponds to a certain rule. The checks are grouped in two categories: XML Violations and XMLSchema Violations. The checks are derived from the error and warning messages provided by the Xerces parser. In the rules profile, you can disable checks or provide another priority for a check. Please note that the XmlSchemaCheck must always be active.

...

The XPath expression language is extremely powerful. It would be possible to create rich libraries of checks using XPath expressions. The XPath Check can be instantiated multiple times in a rules profile.

Examples

XHTML 

Quickly validate a set of XHTML files for validity. The output of the analysis is comparable with online HTML validators. The xhtml files must be reasonably wellformed. The parser is capable to handle documents that are not wellformed (e.g. tags not closed).  

Save your XHTML files with extension .html to the folder src/main/web and create the following maven pom file:

...

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>my.group.id</groupId>
  <artifactId>xml</artifactId>
  <version>1.0</version>
  <name>MyProject :: Xml</name>

  <properties>
    <sonar.language>xml</sonar.language>
    <sonar.xml.sourceDirectory>src/main/web</sonar.xml.sourceDirectory>
    <sonar.xml.fileExtensions>html</sonar.xml.fileExtensions>
    <sonar.xml.schemas>autodetect</sonar.xml.schemas>
   </properties>
</project>

With this pom, you can simply call mvn:

Code Block
mvn sonar:sonar

Maven POM Validation

Maven POM Validation per project

Recursively check the pom file in a project and its subprojects. The plugin has a schema for maven pom file that follows the code conventions as documented by the maven team here. The maven schema was adopted to validate for the order of the elements as documented in the code conventions. 

...

sourceDirectory is set to . means all files in the project are scanned.
includeFileFilter specifies to scan only pom.xml files (relative path to project root). So only the pom file in the current project will be scanned. 
schemas is set to maven namespace so all pom files will be validated with the maven xml schema.

Maven POM Validation (quick)

Quickly validate all pom files in a project and its subprojects. This method is faster than the above method because it does not recurse into each child module. 

...