How to sum the values which satisfy the specific condition for each group of nodes Using XSLT-1.0

南笙酒味 提交于 2019-12-11 15:29:00

问题


how to sum the values which satisfy the specific condition inside a group for my test sample.The following xml data is an input sample.

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

The above elements are linked up through attribute references.

My .xml file contains 4 types of tag elements.

Element-1 (Product) : It contains the attributes named as id, PartNumber and serial number.

Element-2 (RelationalRef) : It contains attribute named as ‘relatedRef ‘in which first half represents ‘product element id’ and second half represents ‘CharValue element id’.

Element-3 (CharValue) : It contains attribute named as ‘defRef’ which represents ‘CharDef element id’.

Element-4 (CharDef) : It contains the attributes named as ‘id, name’.

Against each part number in Product element , corresponding serial number and cumulative ‘CharValue’ to be printed in the corresponding ‘CharDef name’ columns.

I have tried with the following xslt program.

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

Iam getting the following output

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

But i need the following output

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

I think this is possible through sub-grouping inside grouping. I have done first-grouping using the match-pattern as 'RelationalRef' and first-half 'relatedRef' as expression in the first key. But iam not getting which one i have to use for second key so that i can achieve the required output.

Please anybody can provide suggestions to incorporate changes in my xslt code to achieve the required output.

来源:https://stackoverflow.com/questions/55218223/how-to-sum-the-values-which-satisfy-the-specific-condition-for-each-group-of-nod

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!