0

我是全世界在 xslt 中编写代码的新手。我希望 xsl 代码查看我的 xml 文件中的每个节点/属性并搜索“__(string)”并计算“result=success/failed”。我在这个站点上找到了一个线程来帮助我一点: 你如何使用 XSLT 进行通配符匹配?
我得到了我的 xsl 代码来查找字符串匹配。但是我的问题是,我不知道如何编写代码来计算给定字符串匹配的成功和失败?我希望输出看起来像这样:

     **(Casename)**  |  **(total success)** | **(total failed)**
     __(stringmatch1)|         2            |        0
     __(stringmatch2)|         0            |        2

这是 xml 文件的示例:

<report>
     <programtest user="testuser">
           <programtest software="test">
               <programtest testname="SW log">
               </programtest>
               <programtest testname="HW log">
               </programtest>
               <programtest testname="loop_program_test">
                  <programtest casename="test" iteration="1" result="success">
                     <programtest casename="__temp1" type="specifictestcase" result="success" >
                      </programtest>
                      <programtest casename="__temp2" type="specifictestcase" result="failed" >
                      </programtest>
                  </programtest>
                  <programtest casename="test" iteration="2" result="success">
                      <programtest casename="__temp1" type="specifictestcase" result="success" >
                      </programtest>
                      <programtest casename="__temp2" type="specifictestcase" result="failed" >
                      </programtest>
                  </programtest>
               </programtest>
           </programtest>
     </programtest>
</report>

任何帮助表示赞赏!:-)

4

2 回答 2

1

做得好

<xsl:variable name="matchedElements" select="//*[starts-with(@casename, '__')]"/>
<xsl:variable name="unmatchedElements" select="//*[not(starts-with(@casename, '__'))]"/>

您可以找到元素,然后可以在需要时输出数字,例如

<table>
  <thead>
    <tr>
     <th>matched</th>
     <th>not matched</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><xsl:value-of select="count($matchedElements)"/></td>
      <td><xsl:value-of select="count($unmatchedElements)"/></td>
    </tr>
  </tbody>
</table>

这有帮助吗?

我只是简单地使用过starts-with(@casename, '__'),根据你的需要你当然可以使用其他测试,比如@casename = '__temp1'matches(@casename, 'some regular expression pattern here')

于 2012-06-04T09:22:25.497 回答
1

我认为这是一个分组问题。您需要按每个特定programtest元素的 @casename 属性对结果进行分组。在 XSLT2.0 中,您可以通过xsl:for-each-group元素来执行此操作

<xsl:for-each-group select=".//programtest[@type='specifictestcase']" group-by="@casename">

然后,对于每个组,你会得到这样的成功次数

<xsl:value-of select="count(current-group()[@result='succes'])"/>

(注意您的 XML 中可能有错字,不应该是“成功”而不是“成功”吗?)

这是完整的 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html" indent="yes"/>
   <xsl:template match="/report">
      <table>
         <tr>
            <th>**(Casename)**</th>
            <th>**(total success)**</th>
            <th>**(total failed)**</th>
         </tr>
         <xsl:for-each-group select=".//programtest[@type='specifictestcase']" group-by="@casename">
            <tr>
               <td>
                  <xsl:value-of select="current-grouping-key()"/>
               </td>
               <td>
                  <xsl:value-of select="count(current-group()[@result='succes'])"/>
               </td>
               <td>
                  <xsl:value-of select="count(current-group()[@result='failed'])"/>
               </td>
            </tr>
         </xsl:for-each-group>
      </table>
   </xsl:template>
</xsl:stylesheet>

应用于您的示例 XML 时,将输出以下内容

<table>
   <tr>
      <th>**(Casename)**</th>
      <th>**(total success)**</th>
      <th>**(total failed)**</th>
   </tr>
   <tr>
      <td>__temp1</td>
      <td>2</td>
      <td>0</td>
   </tr>
   <tr>
      <td>__temp2</td>
      <td>0</td>
      <td>2</td>
   </tr>
</table>
于 2012-06-04T10:01:24.830 回答