XSLT 1.0统计关联测试用例的接口数量问题求助
统计关联测试用例的接口总数(XSLT 1.0)
需求
统计interface_list.xml中满足以下条件的接口总数:
- 接口ID被包含在测试用例的
extendedinfo字段中 - 该接口关联的测试用例数量大于0
当前使用XSLT 1.0与Sablotron,尝试双重count()函数实现未成功。
原代码与测试数据
原XSLT模板
<xsl:template match="/testmodule" mode="testedinterfaces"> <xsl:variable name="testmodule" select="." /> <xsl:for-each select="$interface_list/interfaces/interface"> <xsl:variable name="nInterfacesTested"> <xsl:value-of select="count(count($testmodule/testgroup/testcase[contains(extendedinfo, current()/ID)])>0)"/> </xsl:variable> </xsl:for-each> <tr> <td class="DefineCell">Overall number of tested interfaces </td> <td class="NumberCell" width="60"><xsl:value-of select="$nInterfacesTested"/></td> <td class="DefaultCell"/> </tr> </xsl:template>
测试数据XML
<testmodule starttime="2022-07-27 16:29:54" timestamp="1397.491492" verdicts="2_basic" measurementid="ad20a6c0"> <testgroup> <testcase starttime="2022-07-27 16:29:54" timestamp="1397.491492"> <extendedinfo type="text">[12345][654321][123654]</extendedinfo> </testcase> <testcase starttime="2022-07-27 16:30:18" timestamp="1421.291492"> <extendedinfo type="text">[12345]</extendedinfo> </testcase> <testcase starttime="2022-07-27 16:30:42" timestamp="1445.091492"> <extendedinfo type="text">[654321]</extendedinfo> </testcase> <testcase starttime="2022-07-27 16:31:06" timestamp="1468.891492"> <extendedinfo type="text">[123654]</extendedinfo> </testcase> </testgroup> </testmodule>
interface_list.xml内容
<header_xml> <interface>12345</interface> <interface>654321</interface> <interface>123654</interface> <interface>112233</interface> <interface>999999</interface> <interface>888888</interface> </header_xml>
预期结果:4
问题分析
- 路径错误:原模板中
$interface_list/interfaces/interface路径不匹配interface_list.xml的结构,实际根节点为header_xml,正确路径应为$interface_list/header_xml/interface。 - 变量作用域问题:
nInterfacesTested在for-each循环内定义,每次循环都会重置变量值,最终仅保留最后一次循环的结果,无法实现累加统计。 - 语法错误:双重
count()的写法不符合XSLT语法,count(count(...))无法正确判断测试用例数量是否大于0。
解决方案
利用XSLT 1.0的节点筛选与count()函数,直接统计符合条件的接口节点数量:
<xsl:template match="/testmodule" mode="testedinterfaces"> <!-- 预存所有测试用例的extendedinfo节点 --> <xsl:variable name="all-testcase-info" select="testgroup/testcase/extendedinfo" /> <!-- 筛选出存在关联测试用例的接口 --> <xsl:variable name="tested-interfaces" select="$interface_list/header_xml/interface[ count($all-testcase-info[contains(., concat('[', normalize-space(.), ']'))]) > 0 ]" /> <tr> <td class="DefineCell">Overall number of tested interfaces </td> <td class="NumberCell" width="60"> <xsl:value-of select="count($tested-interfaces)" /> </td> <td class="DefaultCell"/> </tr> </xsl:template>
说明
- 精确匹配:使用
concat('[', normalize-space(.), ']')生成带包裹符的接口ID,避免部分匹配(如接口123被误匹配到[1234])。 - 高效统计:通过节点谓词直接筛选符合条件的接口,再用
count()统计数量,无需循环累加,符合XSLT 1.0的语法规范。 - 变量复用:预存所有
extendedinfo节点,避免重复遍历测试用例,提升处理效率。
验证
根据提供的测试数据,该模板会统计出12345、654321、123654三个关联接口,若预期结果为4,需检查是否存在第四个接口的ID被测试用例的extendedinfo包含。
内容的提问来源于stack exchange,提问作者Tomek Wojcik
相关产品推荐
相关产品推荐

