2
來自:XSLT Sorting - how to sort xml childnodes inside a parent node with an attribute如何對Jenkins的CppCheck XML輸出進行排序?
我正在使用Jenkins的CppCheck插件來解析XML日誌文件。然而,CppCheck將所有內容放在一起,而不是以任何方式排序。我想通過'location'標籤對XML日誌文件進行排序。
TL;博士:XML層次是「結果/錯誤/錯誤/位置」,我想排序根據「位置/ @文件」和「位置/ @行」
我「錯誤」使用此:
@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 '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 < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&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 '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 < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&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 < 10; ++n) { // it is possible but not safe to move 'int i = 0;' here do_something(&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轉換的新手,所以任何幫助都是值得歡迎的。
感謝您的反饋...下列選項