【问题标题】:Count() inside for-eachCount() inside for-each
【发布时间】:2023-01-17 21:20:07
【问题描述】:

我对 xslt 样式表不是很熟悉,我需要一些帮助。一般来说,目标是检查“测试用例”集中有多少次“接口”出现。我的想法是迭代包含文件 interfaces_list.xml,每次迭代使用 count 函数和 contain 来检查特定接口的出现次数出现在所有测试用例的扩展信息中。可能问题出在上下文上,但我不知道如何将它暂时从“接口”更改为“测试模块”。我尝试了如下代码,但计数为 0。

<xsl:variable name="interface_list" select="document('interface_list.xml')" />
            
<xsl:for-each select="$interface_list/header_xml/interface">
<xsl:variable name="nameofinterface"><xsl:value-of select="."/></xsl:variable>
    <tr>
    <td class="NumberCell" width="60"><xsl:value-of select="count(//testcase/extendedinfo[contains(.,$nameofinterface)])"/></td>            
        </tr>               
</xsl:for-each>
here I want o count occurrences:            
<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>
file: interface_list.xml
<header_xml>

<interface>12345</interface>
<interface>654321</interface>
<interface>123654</interface>
<interface>112233</interface>

</header_xml>

【问题讨论】:

  • 您将为此使用哪个 XSLT 处理器?
  • 我正在使用 sabcmd
  • 对不起,我不熟悉这样的处理器。它支持哪个版本的 XSLT(参见:stackoverflow.com/a/25245033/3016153)?
  • 它是版本 1,供应商是 Ginger Alliance
  • 那是萨布洛特隆吗?我还以为灭绝了。。。

标签: xml xslt


【解决方案1】:

假设您仅限于 XSLT 1.0(没有扩展),您可以这样做:

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:param name="interface_list" select="document('interface_list.xml')" />

<xsl:template match="/testmodule">
    <xsl:variable name="testmodule" select="." />
    <table>
        <xsl:for-each select="$interface_list/header_xml/interface">    
            <tr>
                <td>
                    <xsl:value-of select="."/>
                </td>
                <td>
                    <xsl:value-of select="count($testmodule/testgroup/testcase[contains(extendedinfo, current())])"/>
                </td>            
            </tr>               
        </xsl:for-each>
    </table>
</xsl:template>

</xsl:stylesheet>

要得到:

结果

<?xml version="1.0" encoding="UTF-8"?>
<table>
  <tr>
    <td>12345</td>
    <td>2</td>
  </tr>
  <tr>
    <td>654321</td>
    <td>2</td>
  </tr>
  <tr>
    <td>123654</td>
    <td>2</td>
  </tr>
  <tr>
    <td>112233</td>
    <td>0</td>
  </tr>
</table>

请注意,这假定一个 interface 值不能包含在另一个值中。如果这个假设不正确(正如我在注意到它们的长度不完全相同后所怀疑的那样),则将上面的计数指令更改为:

<xsl:value-of select="count($testmodule/testgroup/testcase[contains(extendedinfo, concat('[', current(), ']'))])"/>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-24
  • 2022-07-22
  • 2013-09-27
  • 2018-09-11
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
相关资源
最近更新 更多