0

以下是 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
4

2 回答 2

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>
于 2013-09-20T14:41:48.440 回答
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>
于 2013-09-20T19:02:33.570 回答