简体   繁体   中英

Counting using XSLT

Following is the XML file, Using XSL I need to generate report


 <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>

With the above XML file need to generate report using XSL as shown below. Need a method to count and produce below outputs.

 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

You can use something similar to below.

<?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>

Here is a solution that is not hardwired for the artifacts and owners:

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>

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>

Stylesheet:

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM