From : XSLT Sorting - 親ノード内のxml子ノードを属性でソートする方法
XML ログ ファイルを解析する Jenkins の CppCheck プラグインを使用しています。ただし、CppCheck はすべてをそのまま配置し、ソートされません。XML ログ ファイルを「場所」タグで並べ替えたいと思います。
tl;dr : XML 階層は 'results/errors/error/location' です。'location/@file' および 'location/@line' に従って 'error' をソートしたい
私はこれを使用しています:
@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は最初のエラーメッセージを1行で平らにします):
<?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 変換の新人なので、どんな助けも歓迎します。
フィードバックをお寄せいただきありがとうございます...