Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
{iframe:src=http://updateredirect.sonarsource.orgcom/plugins/xml.html|width=700|height=250300|frameborder=0}
YourDocumentation browserhas doesbeen notmoved support iframes.
{iframe}

Description / Features

Any XML file can be validated with this plugin. The plugin provides static code analysis of XML files within Sonar. Typical use cases are validation of files for coding or usability standards. 

The following functionality is provided:

  • XML validation
  • XML schema validation
  • Configurable rules using XPath
  • sizing (files, lines of code)

The XML validation is available out-of-the box. The configurable XPath rules requires some knowledge of XPath.

The plugin scans the following files by default: .xml, .xhtml. The file extensions and the file inclusion patterns are configurable. 

The plugin imports the XML files in Sonar, and performs the validations. The checks are configurable in the Sonar rules repository. 

Use Cases

Typical use cases:

- validation of XHTML files

By using the XHTML schemas as provided by the W3C (strict, transitional, and others) quickly validate the markup of a set of XHTML files. 

- validation of XML files

Validate a set of XML files for conformance to the provided XML schema. 

- Validate rules in XML files 

e.g. validate the maven pom files for conformance to coding standards.

Installation

  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

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.

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

 

 

Schema Validation

XmlSchemaCheck

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

The check has two properties: 

property

description

description

filePattern

filter for the files to be validated with this check

 

schemas

namespace of schema to use for validation

e.g. xhtml1-strict for a built-in schema
or
schemas/xsd/mycustomschema.xsd for a schema on filesystem

If you specify schemas=autodetect the parser will try to load the schema based on the doctype or the namespace declaration in the document.

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:

namespace or short name

Doctype

Description

http://www.w3.org/2001/xml.xsd

 

XML

http://www.w3.org/1999/xhtml

 

XHTML1.0 Strict

xhtml1-strict

-//W3C//DTD XHTML 1.0 Strict//EN

XHTML1.0 Strict

xhtml1-transitional

-//W3C//DTD XHTML 1.0 Transitional//EN

XHTML1.0Ttransitional

xhtml1-frameset

-//W3C//DTD XHTML 1.0 Frameset//EN

XHTML1.0 Frameset

 

-//W3C//DTD XHTML 1.1 Strict//EN

XHTML1.1

http://java.sun.com/jsf/core

 

JSF core

http://java.sun.com/jsf/html

 

JSF HTML Basic

http://java.sun.com/jsf/facelets

 

JSF Facelets

http://maven.apache.org/POM/4.0.0

 

Maven Project Model

Notes:

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

XPath Check

The XPath Check allows to specify checks on XML documents using XPath expressions. 

The purpose of the check is to look for nodes and attributes that are not allowed. In Sonar, you can create the XPath rules while editing the rules profile.

In Sonar, goto the configuration pages where you can edit the Quality Profiles. The first step is to make a copy of the Default XML profile so you can start editing the rules in your own profile. Now select the XPath rule. The rule has two properties: expression for the XPath expression, and filePattern to specify files to be included (e.g. */myfiles/.xml). The XPath expression should look for nodes or attributes that are not allowed.

In the quality profile you can make multiple XPath rules, in the same way as is done for PMD, see this page for documentation: Quality Profiles

Example expressions:

expression

description

//b

Find all occurrences of <b>

//td[@nowrap]

Find all occurrences of td with the deprecated attribute nowrap

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.

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. 

Code Block
mvn sonar:sonar -Dmaven.test.skip=true -Dsonar.language=xml -Dsonar.xml.sourceDirectory=. -Dsonar.xml.includeFileFilter=pom.xml -Dsonar.xml.schemas=http://maven.apache.org/POM/4.0.0

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. 

Code Block
mvn -N sonar:sonar -Dmaven.test.skip=true -Dsonar.language=xml -Dsonar.xml.sourceDirectory=. -Dsonar.xml.includeFileFilter=**/pom.xml -Dsonar.xml.schemas=http://maven.apache.org/POM/4.0.0

-N is specified so only the root project will be analyzed by maven.
sourceDirectory is set to . means all files in the project are scanned.
includeFileFilter specifies to scan all pom.xml files. With this filter, also pom files in child modules will be scanned. 
schemas is set to maven namespace so all pom files will be validated with the maven xml schema.

Plugin Architecture

The plugin uses the Xerces library for parsing and validation.  

http://xerces.apache.org/xerces-j/

Change Log

JIRA Issues
anonymoustrue
titleRelease 0.2
width800
columnstype;key;summary;priority;status;resolution
urlhttp://jira.codehaus.org/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?fixfor=17353&pid=11911&sorter/field=priority&sorter/order=DESC&tempMax=1000

 

JIRA Issues
anonymoustrue
titleRelease 0.1
width800
columnstype;key;summary;priority;status;resolution
urlhttp://jira.codehaus.org/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?fixfor=17345&pid=11911&sorter/field=priority&sorter/order=DESC&tempMax=1000

to [http://redirect.sonarsource.com/plugins/xml.html].
{iframe}