如何使用XSLT-1.0对每组节点满足特定条件的值求和

时间:2019-03-18 09:30:57

标签: xslt-1.0

如何对测试样本的组中满足特定条件的值求和。以下xml数据是输入样本。

<?xml version="1.0" encoding="UTF-8"?>

<Root>

    <Product id="id111" partNumber="LRU1" serialNumber="xyz1"> </Product>
    <Product id="id112" partNumber="LRU2" serialNumber="xyz2"> </Product>
    <Product id="id113" partNumber="LRU3" serialNumber="xyz3"> </Product>
    <Product id="id114" partNumber="LRU4" serialNumber="xyz4"> </Product>
    <Product id="id115" partNumber="LRU5" serialNumber="xyz5"> </Product>

    <RelationalRef id="id211" relatedRef="#id111 #id311"> </RelationalRef>
    <RelationalRef id="id212" relatedRef="#id111 #id312"> </RelationalRef>
    <RelationalRef id="id213" relatedRef="#id111 #id313"> </RelationalRef>
    <RelationalRef id="id214" relatedRef="#id111 #id314"> </RelationalRef>
    <RelationalRef id="id215" relatedRef="#id111 #id315"> </RelationalRef>
    <RelationalRef id="id216" relatedRef="#id111 #id316"> </RelationalRef>
    <RelationalRef id="id217" relatedRef="#id111 #id317"> </RelationalRef>
    <RelationalRef id="id218" relatedRef="#id111 #id318"> </RelationalRef>
    <RelationalRef id="id219" relatedRef="#id111 #id319"> </RelationalRef>
    <RelationalRef id="id220" relatedRef="#id111 #id320"> </RelationalRef>
    <RelationalRef id="id221" relatedRef="#id111 #id321"> </RelationalRef>
    <RelationalRef id="id222" relatedRef="#id111 #id322"> </RelationalRef>
    <RelationalRef id="id223" relatedRef="#id112 #id323"> </RelationalRef>
    <RelationalRef id="id224" relatedRef="#id112 #id324"> </RelationalRef>
    <RelationalRef id="id225" relatedRef="#id112 #id325"> </RelationalRef>
    <RelationalRef id="id226" relatedRef="#id112 #id319"> </RelationalRef>
    <RelationalRef id="id227" relatedRef="#id112 #id321"> </RelationalRef>
    <RelationalRef id="id228" relatedRef="#id112 #id322"> </RelationalRef>
    <RelationalRef id="id229" relatedRef="#id113 #id326"> </RelationalRef>
    <RelationalRef id="id230" relatedRef="#id113 #id327"> </RelationalRef>
    <RelationalRef id="id231" relatedRef="#id113 #id316"> </RelationalRef>
    <RelationalRef id="id232" relatedRef="#id113 #id318"> </RelationalRef>
    <RelationalRef id="id233" relatedRef="#id113 #id320"> </RelationalRef>
    <RelationalRef id="id234" relatedRef="#id113 #id322"> </RelationalRef>
    <RelationalRef id="id235" relatedRef="#id114 #id328"> </RelationalRef>
    <RelationalRef id="id236" relatedRef="#id114 #id329"> </RelationalRef>
    <RelationalRef id="id237" relatedRef="#id114 #id319"> </RelationalRef>
    <RelationalRef id="id238" relatedRef="#id114 #id322"> </RelationalRef>
    <RelationalRef id="id239" relatedRef="#id115 #id330"> </RelationalRef>
    <RelationalRef id="id240" relatedRef="#id115 #id331"> </RelationalRef>
    <RelationalRef id="id241" relatedRef="#id115 #id316"> </RelationalRef>
    <RelationalRef id="id242" relatedRef="#id115 #id317"> </RelationalRef>
    <RelationalRef id="id243" relatedRef="#id115 #id320"> </RelationalRef>
    <RelationalRef id="id244" relatedRef="#id115 #id321"> </RelationalRef>

    <CharValue id="id311" defRef="#id411" value="2"> </CharValue>
    <CharValue id="id312" defRef="#id412" value="1"> </CharValue>
    <CharValue id="id313" defRef="#id413" value="2"> </CharValue>
    <CharValue id="id314" defRef="#id414" value="120"> </CharValue>
    <CharValue id="id315" defRef="#id411" value="1"> </CharValue>
    <CharValue id="id316" defRef="#id412" value="2"> </CharValue>
    <CharValue id="id317" defRef="#id413" value="4"> </CharValue>
    <CharValue id="id318" defRef="#id414" value="50"> </CharValue>
    <CharValue id="id319" defRef="#id411" value="3"> </CharValue>
    <CharValue id="id320" defRef="#id412" value="1"> </CharValue>
    <CharValue id="id321" defRef="#id413" value="1"> </CharValue>
    <CharValue id="id322" defRef="#id414" value="80"> </CharValue>
    <CharValue id="id323" defRef="#id411" value="2"> </CharValue>
    <CharValue id="id324" defRef="#id413" value="4"> </CharValue>
    <CharValue id="id325" defRef="#id414" value="150"> </CharValue>
    <CharValue id="id326" defRef="#id412" value="6"> </CharValue>
    <CharValue id="id327" defRef="#id414" value="600"> </CharValue>
    <CharValue id="id328" defRef="#id411" value="25"> </CharValue>
    <CharValue id="id329" defRef="#id414" value="750"> </CharValue>
    <CharValue id="id330" defRef="#id412" value="2"> </CharValue>
    <CharValue id="id331" defRef="#id413" value="300"> </CharValue>

    <CharDef id="id411" name="NoOfLandings"> </CharDef>
    <CharDef id="id412" name="NoOfStreams"> </CharDef>
    <CharDef id="id413" name="NoOfStarts"> </CharDef>
    <CharDef id="id414" name="consumedLifeInMins"> </CharDef>

</Root>

以上元素通过属性引用链接在一起。

我的.xml文件包含4种标记元素。

Element-1(产品):它包含名为id,PartNumber和序列号的属性。

Element-2(RelationalRef):它包含名为“ relatedRef”的属性,其中前半部分表示“产品元素ID”,后半部分表示“ CharValue元素ID”。

Element-3(CharValue):它包含名为“ defRef”的属性,该属性表示“ CharDef元素ID”。

Element-4(CharDef):它包含名为“ id,name”的属性。

针对Product元素中的每个零件号,相应的序列号以及要在相应的“ CharDef名称”列中打印的累积“ CharValue”。

我尝试了以下xslt程序。

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

    <xsl:output method="html" indent="yes"/>    

    <xsl:key name="groupByRefId" match="RelationalRef" use="substring-before(@relatedRef,' ')"/>

    <xsl:template match="/">
      <html>
        <body>
          <p align="center">Consumed Report</p>
          <xsl:call-template name="TableHeadings"/>
        </body>
      </html>
    </xsl:template>

  <xsl:template name="TableHeadings">
      <table border="1" width="90%">
        <tr>
          <th align="center">Part Number</th>
          <th align="center">Serial Number</th>
          <th align="center">No. of Landings</th>
          <th align="center">No. of Streams</th>
          <th align="center">No. of Starts</th>
          <th align="center">Consumed Life in Mins</th>
        </tr>
        <xsl:call-template name="ProcessLrus"/>
      </table>
  </xsl:template>

  <xsl:template name="ProcessLrus">
    <xsl:for-each select="//RelationalRef[generate-id(.)=generate-id(key('groupByRefId',substring-before(@relatedRef,' '))[1])]">
      <xsl:variable name="productId" select="substring-before(@relatedRef,' ')"/>

      <tr>

      <td>
        <xsl:value-of select="//Product[concat('#',@id)=$productId]/@partNumber"/>
      </td>       

      <td>
        <xsl:value-of select="//Product[concat('#',@id)=$productId]/@serialNumber"/>
      </td>     

        <xsl:variable name="cumValueForLandings">
      <xsl:for-each select="key('groupByRefId',substring-before(@relatedRef,' '))">
        <xsl:variable name="charValueId" select="substring-after(@relatedRef,' #')"/>
        <xsl:copy-of select="sum(//CharValue[@id=$charValueId and @defRef='#id411']/@value)"/>
      </xsl:for-each>
        </xsl:variable> 

        <td>
          <xsl:value-of select="$cumValueForLandings"/> 
        </td>     

        <xsl:variable name="cumValueForStreams">
          <xsl:for-each select="key('groupByRefId',substring-before(@relatedRef,' '))">
            <xsl:variable name="charValueId" select="substring-after(@relatedRef,' #')"/>
            <xsl:copy-of select="sum(//CharValue[@id=$charValueId and @defRef='#id412']/@value)"/>
          </xsl:for-each>
        </xsl:variable>   

        <td>
          <xsl:value-of select="$cumValueForStreams"/>
        </td>  

       <xsl:variable name="cumValueForStarts">
          <xsl:for-each select="key('groupByRefId',substring-before(@relatedRef,' '))">
            <xsl:variable name="charValueId" select="substring-after(@relatedRef,' #')"/>
            <xsl:copy-of select="sum(//CharValue[@id=$charValueId and @defRef='#id413']/@value)"/>
          </xsl:for-each>
       </xsl:variable>   

        <td>
          <xsl:value-of select="$cumValueForStarts"/>
        </td>       

      </tr> 

    </xsl:for-each>
  </xsl:template>  
</xsl:stylesheet>

我得到以下输出

    <html>
   <body>
      <p align="center">Consumed Report</p>
      <table border="1" width="90%">
         <tr>
            <th align="center">Part Number</th>
            <th align="center">Serial Number</th>
            <th align="center">No. of Landings</th>
            <th align="center">No. of Streams</th>
            <th align="center">No. of Starts</th>
            <th align="center">Consumed Life in Mins</th>
         </tr>
         <tr>
            <td>LRU1</td>
            <td>xyz1</td>
            <td>2 0 0 0 1 0 0 0 3 0 0 0</td>
            <td>0 1 0 0 0 2 0 0 0 1 0 0</td>
            <td>0 0 2 0 0 0 4 0 0 0 1 0</td>
         </tr>
         <tr>
            <td>LRU2</td>
            <td>xyz2</td>
            <td>2 0 0 3 0 0</td>
            <td>0 0 0 0 0 0</td>
            <td>0 4 0 0 1 0</td>
         </tr>
         <tr>
            <td>LRU3</td>
            <td>xyz3</td>
            <td>0 0 0 0 0 0</td>
            <td>6 0 2 0 1 0</td>
            <td>0 0 0 0 0 0</td>
         </tr>
         <tr>
            <td>LRU4</td>
            <td>xyz4</td>
            <td>25 0 3 0</td>
            <td>0 0 0 0</td>
            <td>0 0 0 0</td>
         </tr>
         <tr>
            <td>LRU5</td>
            <td>xyz5</td>
            <td>0 0 0 0 0 0</td>
            <td>2 0 2 0 1 0</td>
            <td>0 300 0 4 0 1</td>
         </tr>
      </table>
   </body>
</html>

但是我需要以下输出

<html>
   <body>
      <p align="center">Consumed Report</p>
      <table border="1" width="90%">
         <tr>
            <th align="center">Part Number</th>
            <th align="center">Serial Number</th>
            <th align="center">No. of Landings</th>
            <th align="center">No. of Streams</th>
            <th align="center">No. of Starts</th>
            <th align="center">Consumed Life in Mins</th>
         </tr>
         <tr>
            <td>LRU1</td>
            <td>xyz1</td>
            <td>6</td>
            <td>4</td>
            <td>7</td>
         </tr>
         <tr>
            <td>LRU2</td>
            <td>xyz2</td>
            <td>5</td>
            <td>0</td>
            <td>5</td>
         </tr>
         <tr>
            <td>LRU3</td>
            <td>xyz3</td>
            <td>0</td>
            <td>9</td>
            <td>0</td>
         </tr>
         <tr>
            <td>LRU4</td>
            <td>xyz4</td>
            <td>28</td>
            <td>0</td>
            <td>0</td>
         </tr>
         <tr>
            <td>LRU5</td>
            <td>xyz5</td>
            <td>0</td>
            <td>5</td>
            <td>305</td>
         </tr>
      </table>
   </body>
</html>

我认为可以通过分组内部的分组来实现。我已经完成了使用match-pattern作为“ RelationalRef”和上半“ relatedRef”作为第一个键中的表达式的第一次分组。但是我无法获得第二个键必须使用的哪个键,这样我才能获得所需的输出。

请任何人都可以提供建议,以将更改合并到我的xslt代码中以实现所需的输出。

0 个答案:

没有答案