使用XSLT计数

时间:2013-09-20 13:06:32

标签: xml xslt count nodes

以下是XML文件,使用XSL我需要生成报告


 <results>   
 <result_header name="cpu.log">      
  <owner>VJ </owner>
  <artifact>cpu </artifact>
  <status>PASS</status>
 </result_header>
 <result_header name="mem.log">
  <owner>BG </owner>
  <artifact>mem </artifact>
  <status>PASS</status>
 </result_header>
 <result_header name="dma.log">
  <owner>VJ </owner>
  <artifact>dma </artifact>
  <status>PASS</status>
 </result_header>
 <result_header name="dma0.log">
   <owner>VJ </owner>
   <artifact>dma </artifact>
   <status>PASS</status>
 </result_header>
  <result_header name="dma1.log">
    <owner>VJ </owner>
    <artifact>dma </artifact>
    <status>FAIL</status>
 </result_header>
 </results>

使用上面的XML文件需要使用XSL生成报告,如下所示。  需要一种计算和生成以下输出的方法。

 Need output

 artifact : Total  : Count Pass : Count Fail
 CPU      1         1           0
 DMA      3         2           1
 MEM      1         1           0


  Owner : Total  : Count Pass : Count Fail
  BG       1         1           0  
  VJ       4         4           1

2 个答案:

答案 0 :(得分:0)

您可以使用类似于下面的内容。

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes"/>

<xsl:template match="results">

<table>
<tr>
 <td>
  artifact : Total 
 </td>
 <td>
Count Pass
 </td>
 <td>
 Count Fail
 </td>
 </tr>
 <tr>
 <td>
<xsl:value-of select="count(/results/result_header[artifact='dma'])"/> 
 </td>
 <td>
<xsl:value-of select="count(/results/result_header[artifact='dma'][status='PASS'])"/> 
 </td>
 <td>
<xsl:value-of select="count(/results/result_header[artifact='dma'][status='FAIL'])"/> 
 </td>
</tr>
</table>

答案 1 :(得分:0)

这是一个不为工件和所有者提供硬连线的解决方案:

t:\ftemp>type status.xml 
<?xml version="1.0" encoding="UTF-8"?>
 <results>   
 <result_header name="cpu.log">      
  <owner>VJ </owner>
  <artifact>cpu </artifact>
  <status>PASS</status>
 </result_header>
 <result_header name="mem.log">
  <owner>BG </owner>
  <artifact>mem </artifact>
  <status>PASS</status>
 </result_header>
 <result_header name="dma.log">
  <owner>VJ </owner>
  <artifact>dma </artifact>
  <status>PASS</status>
 </result_header>
 <result_header name="dma0.log">
   <owner>VJ </owner>
   <artifact>dma </artifact>
   <status>PASS</status>
 </result_header>
  <result_header name="dma1.log">
    <owner>VJ </owner>
    <artifact>dma </artifact>
    <status>FAIL</status>
 </result_header>
 </results>

结果:

t:\ftemp>call xslt2 status.xml status.xsl 
<?xml version="1.0" encoding="UTF-8"?>
<tr>
   <th>artifact</th>
   <th>Total</th>
   <th>Count Pass</th>
   <th>Count Fail
</th>
</tr>
<tr>
   <td>cpu </td>
   <td>1</td>
   <td>1</td>
   <td>0</td>
</tr>
<tr>
   <td>dma </td>
   <td>3</td>
   <td>2</td>
   <td>1</td>
</tr>
<tr>
   <td>mem </td>
   <td>1</td>
   <td>1</td>
   <td>0</td>
</tr>
<tr>
   <td>BG </td>
   <td>1</td>
   <td>1</td>
   <td>0</td>
</tr>
<tr>
   <td>VJ </td>
   <td>4</td>
   <td>3</td>
   <td>1</td>
</tr>

样式表:

t:\ftemp>type status.xsl 
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="results">
  <tr>
    <th>artifact</th>
    <th>Total</th>
    <th>Count Pass</th>
    <th>Count Fail&#xa;</th>
  </tr>

  <xsl:for-each-group select="result_header" group-by="artifact">
    <xsl:sort select="artifact"/>
    <xsl:call-template name="bodyRows"/>
  </xsl:for-each-group>

  <xsl:for-each-group select="result_header" group-by="owner">
    <xsl:sort select="owner"/>
    <xsl:call-template name="bodyRows"/>
  </xsl:for-each-group>
</xsl:template>

<xsl:template name="bodyRows">
  <tr>
    <td>
      <xsl:value-of select="current-grouping-key()"/>
    </td>
    <td>
      <xsl:value-of select="count(current-group())"/>
    </td>
    <td>
      <xsl:value-of select="count(current-group()[status='PASS'])"/>
    </td>
    <td>
      <xsl:value-of select="count(current-group()[status='FAIL'])"/>
    </td>
  </tr>
</xsl:template>

</xsl:stylesheet>