如何为Jenkins排序CppCheck XML输出?

时间:2014-01-10 14:00:44

标签: xml sorting xslt jenkins cppcheck

来自:XSLT Sorting - how to sort xml childnodes inside a parent node with an attribute

我正在使用解析XML日志文件的Jenkins的CppCheck插件。然而,CppCheck会把所有内容都放在一边,而且不会以任何方式排序。我想通过'location'标签对XML日志文件进行排序。

tl; dr:XML层次结构是'结果/错误/错误/位置',我想根据'location / @ file'和'location / @ line'排序'错误'

我正在使用它:

@echo off
"C:\Program Files (x86)\xmlstarlet-1.5.0\xml.exe" tr ".\cppcheck_log.xslt" ".\cppcheck_log.xml" 1>".\cppcheck_log_sorted.xml"

使用以下XLST转换规则:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="errors">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="*">
                <xsl:sort select="@file" data-type="text" order="ascending"/>
                <xsl:sort select="@line" data-type="number" order="ascending"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

但它不起作用。 CppCheck产生的是:

<?xml version="1.0" encoding="UTF-8"?>
<results version="2">
    <cppcheck version="1.63.1"/>
    <errors>
        <error id="variableScope" severity="style" msg="The scope of the variable &apos;i&apos; can be reduced." verbose="The scope of the variable &apos;i&apos; can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for &apos;i&apos; can be reduced:
void f(int x)
{
    int i = 0;
    if (x) {
        // it&apos;s safe to move &apos;int i = 0;&apos; here
        for (int n = 0; n &lt; 10; ++n) {
            // it is possible but not safe to move &apos;int i = 0;&apos; here
            do_something(&amp;i);
        }
    }
}
When you see this message it is always safe to reduce the variable scope 1 level.">
            <location file="toto\test.c" line="50"/>
        </error>
        <error id="unreadVariable" severity="style" msg="Variable 'size' is assigned a value that is never used." verbose="Variable 'size' is assigned a value that is never used.">
            <location file="tata\done.c" line="25"/>
        </error>
    </errors>
</results>

我想得到什么:

<?xml version="1.0" encoding="UTF-8"?>
<results version="2">
    <cppcheck version="1.63.1"/>
    <errors>
        <error id="unreadVariable" severity="style" msg="Variable 'size' is assigned a value that is never used." verbose="Variable 'size' is assigned a value that is never used.">
            <location file="tata\done.c" line="25"/>
        </error>
        <error id="variableScope" severity="style" msg="The scope of the variable &apos;i&apos; can be reduced." verbose="The scope of the variable &apos;i&apos; can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for &apos;i&apos; can be reduced:
void f(int x)
{
    int i = 0;
    if (x) {
        // it&apos;s safe to move &apos;int i = 0;&apos; here
        for (int n = 0; n &lt; 10; ++n) {
            // it is possible but not safe to move &apos;int i = 0;&apos; here
            do_something(&amp;i);
        }
    }
}
When you see this message it is always safe to reduce the variable scope 1 level.">
            <location file="toto\test.c" line="50"/>
        </error>
    </errors>
</results>

我目前得到的仍然是未排序的(XMLStarlet在一行中将第一条错误消息弄平了):

<?xml version="1.0" encoding="UTF-8"?>
<results version="2">
    <cppcheck version="1.63.1"/>
    <errors>
        <error id="variableScope" severity="style" msg="The scope of the variable 'i' can be reduced." verbose="The scope of the variable 'i' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced: void f(int x) {     int i = 0;     if (x) {         // it's safe to move 'int i = 0;' here         for (int n = 0; n &lt; 10; ++n) {             // it is possible but not safe to move 'int i = 0;' here             do_something(&amp;i);         }     } } When you see this message it is always safe to reduce the variable scope 1 level.">
            <location file="toto\test.c" line="50"/>
        </error>
        <error id="unreadVariable" severity="style" msg="Variable 'size' is assigned a value that is never used." verbose="Variable 'size' is assigned a value that is never used.">
            <location file="tata\done.c" line="25"/>
        </error>
    </errors>
</results>

不太好。

我是XML转型的新手,所以欢迎任何帮助。

感谢您的反馈......