我正在使用 gcovr 生成覆寫報告以匯入聲納。這使用這種格式(https://docs.sonarqube.org/latest/analysis/generic-test/):
<coverage version="1">
<file path="src/hello.cpp">
<lineToCover lineNumber="6" covered="true"/>
<lineToCover lineNumber="7" covered="false" branchesToCover="2" coveredBranches="1"/>
</file>
<file path="test/helloTest.cpp">
<lineToCover lineNumber="3" covered="true" branchesToCover="2" coveredBranches="1"/>
</file>
</coverage>
我想包括所有檔案的行覆寫,但我想消除測驗代碼的分支覆寫,因為它只執行“快樂”分支。保持行覆寫顯示了從未執行過的測驗的冗余代碼。有點像這個問題 - https://github.com/gcovr/gcovr/issues/482
基本上我想說:
- 如果路徑屬性與模式 test/* 匹配,則
- 從子項中洗掉branchesToCover 和coveredBranches 屬性
因此,對于上述輸出將變為:
<coverage version="1">
<file path="src/hello.cpp">
<lineToCover lineNumber="6" covered="true"/>
<lineToCover lineNumber="7" covered="false" branchesToCover="2" coveredBranches="1"/>
</file>
<file path="test/helloTest.cpp">
<lineToCover lineNumber="3" covered="true"/>
</file>
</coverage>
這似乎是 XSLT 的理想選擇。不幸的是,這是我還沒有掌握的技能。
我已經能夠弄清楚如何洗掉所有元素的屬性,但不知道如何將其限制為(父)檔案元素中的路徑屬性與某些條件匹配的情況。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@branchesToCover"/>
<xsl:template match="@coveredBranches"/>
</xsl:stylesheet>
如果有人可以解釋如何做到這一點或指出我正確的方向,那將是有幫助和有教育意義的。我想這可能是這些問題的答案的一些組合:
XSLT 1.0:根據與父級屬性匹配的元素屬性洗掉元素
使用基于屬性名稱的 XSLT 過濾屬性
uj5u.com熱心網友回復:
這是可以做到的一種方法:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<!-- Identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Remove branchesToCover attribute when file/@path starts with 'test' -->
<xsl:template match="@branchesToCover[starts-with(ancestor::file/@path,'test')]"/>
<!-- Remove coveredBranches attribute when file/@path starts with 'test' -->
<xsl:template match="@coveredBranches[starts-with(ancestor::file/@path,'test')]"/>
</xsl:stylesheet>
看到它在這里作業:https ://xsltfiddle.liberty-development.net/nbspVbj
uj5u.com熱心網友回復:
我建議你這樣做:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="file[starts-with(@path, 'test/')]/lineToCover">
<xsl:copy>
<xsl:copy-of select="@lineNumber | @covered"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
uj5u.com熱心網友回復:
另一個選擇只是為了好玩...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="file[starts-with(@path,'test/')]/lineToCover/@*[name()='branchesToCover' or name()='coveredBranches']"/>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/444239.html
