Does duplication detection in XML actually work?

I have enabled the XML duplication rule (common-xml:DuplicatedBlocks) but it does not seem to work. There are zero duplications reported while I am sure there exist duplications.

I am using SonarQube version 6.7.0.33306 and SonarXml version 2.0.1 (build 2020).

To reproduce, run the XML duplication rule on a file containing the XML below. Notice it contains a duplication that spans 12 lines with only 1 different XML attribute.

<root>
  <module target-namespace="Platform.Metadata.Meta.Roots">
   <rule name="Create">
      <input name="MetadataContext" required="yes" />
      <action name="Insert" type="Platform.Metadata.Meta.Roots" input-all="yes">
        <argument local-name="MetadataContext" />
        <output local-name="IID" remote-name="iid" />
        <output local-name="TypedIID" remote-name="Platform~TypedIID" />
        <output local-name="ResultObject" remote-name="Platform~ResultObject" />
      </action>
      <output name="IID" attribute="IID" />
      <output name="TypedIID" attribute="TypedIID" />
      <output name="ResultObject" attribute="ResultObject" />
    </rule>
  </module>
  <module target-namespace="Platform.Metadata.Meta.SettingSets">
    <rule name="Create">
      <input name="MetadataContext" required="yes" />
      <action name="Insert" type="Platform.Metadata.Meta.SettingSets" input-all="yes">
        <argument local-name="MetadataContext" />
        <output local-name="IID" remote-name="iid" />
        <output local-name="TypedIID" remote-name="Platform~TypedIID" />
        <output local-name="ResultObject" remote-name="Platform~ResultObject" />
      </action>
      <output name="IID" attribute="IID" />
      <output name="TypedIID" attribute="TypedIID" />
      <output name="ResultObject" attribute="ResultObject" />
    </rule>
  </module>
</root>

Hi,

First of all please consider updating to latest LTS 7.9.x., version you are using is not supported anymore.

About XML duplication detection, XML analysis does not provide this feature. Indeed presence of the rule common-xml:DuplicatedBlocks is confusing, but that’s a standard rule we have for any language (you even have common-xml:FailedUnitTests which for sure does not make any sense).

I will see if we can improve documentation to be explicit about languages not providing duplication detection.

1 Like

Thank you for your fast and clear reply.

Can the XML analysis not be made to detect duplicate code? Is this a feature that can be requested/put on the backlog, or can I contribute to it myself? What is missing from XML analysis to make it work?

Does it make sense to look for “duplicate code” in XML?

  • The main usage of XML is to represent data, not code. Finding duplicate blocks of data in a XML file does not seem relevant to me.
  • Unlike programming languages, XML does not provide a way to factorize blocks. Even when XML is used to represent code, it’s not always possible to avoid duplications.

I think it differs per usage of the XML whether it is relevant or not. So it should be configurable whether duplication detection is enabled or not for XML. It depends on the semantics attached to the XML: if that semantics allows to factorize blocks, duplication detection is relevant. If the semantics doesn’t, duplication detection is not relevant.

In my case, the semantics attached to the XML allows factorizing blocks and duplication detection makes sense. So can’t it be an opt-in feature?

Is there an option that I create a SonarQube plugin myself to get such functionality, if it will not be supported in SonarXml? Are there guidelines/walkthroughs how to create duplicate block detection in a Sonar plugin?

Indeed, you can consider creating a separate plugin for XML duplication block detection.
This plugin should not declare the XML language so that it can work along with sonar-xml-plugin.

I don’t think that there’s any documentation about how to plug a language to CPD (Copy-paste detector). You can have a look at:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.