来自:XSLT 排序 - 如何使用属性对父节点内的 xml 子节点进行排序
我正在使用 Jenkins 的 CppCheck 插件来解析 XML 日志文件。但是 CppCheck 将所有内容按原样放置,并且没有以任何方式排序。我想按“位置”标签对 XML 日志文件进行排序。
tl;dr:XML 层次结构是“结果/错误/错误/位置”,我想根据“位置/@file”和“位置/@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 '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 转换的新手,所以欢迎任何帮助。
感谢您的反馈...