tags:

views:

313

answers:

3

Hi.. I'm a little bit of a rookie when it comes to XSLT and XPath, so I've run into this problem. I'm creating a simple table based on a view on the databse, which needs to sum some of the columns in the view. What I thourght i could do was add 'SUM_' as a prefix of all the coumns that i want to sum, and cut that tag off with a substring when the coulmn name should be shown.

Here's en example of my XML:

<Rowset>
    <Columns>
     <Column Description="SegmentResponseGlobId" MaxRange="1" MinRange="0" Name="SegmentResponseGlobId" SQLDataType="12" SourceColumn="SegmentResponseGlobId"></Column>
     <Column Description="Batch" MaxRange="1" MinRange="0" Name="Batch" SQLDataType="12" SourceColumn="Batch"></Column>
     <Column Description="Start" MaxRange="1" MinRange="0" Name="Start" SQLDataType="93" SourceColumn="Start"></Column>
     <Column Description="Slut" MaxRange="1" MinRange="0" Name="Slut" SQLDataType="93" SourceColumn="Slut"></Column>
     <Column Description="Rute" MaxRange="1" MinRange="0" Name="Rute" SQLDataType="8" SourceColumn="Rute"></Column>
     <Column Description="Tankvogn" MaxRange="1" MinRange="0" Name="Tankvogn" SQLDataType="8" SourceColumn="Tankvogn"></Column>
     <Column Description="SUM_Mængde" MaxRange="1" MinRange="0" Name="SUM_Mængde" SQLDataType="8" SourceColumn="SUM_Mængde"></Column>
     <Column Description="EquipmentId" MaxRange="1" MinRange="0" Name="EquipmentId" SQLDataType="12" SourceColumn="EquipmentId"></Column>
     <Column Description="SLS" MaxRange="1" MinRange="0" Name="SLS" SQLDataType="8" SourceColumn="SLS"></Column>
     <Column Description="PH" MaxRange="1" MinRange="0" Name="PH" SQLDataType="8" SourceColumn="PH"></Column>
    </Columns>
    <Row>
     <SegmentResponseGlobId>AD86D4EC-5E5E-4B6A-A3FC-4BEDF62F3545</SegmentResponseGlobId>
     <Batch>9492002</Batch>
     <Start>2009-12-01T11:13:43</Start>
     <Slut>2009-12-02T19:37:55</Slut>
     <Rute>0</Rute>
     <Tankvogn>6</Tankvogn>
     <SUM_Mængde>1</SUM_Mængde>
     <EquipmentId>A1_C1U11_Udvejning_råmælk</EquipmentId>
     <SLS>0</SLS>
     <PH>NA</PH>
    </Row>
    <Row>
     <SegmentResponseGlobId>28D65598-98D0-41CD-BB6B-6E962834D8F2</SegmentResponseGlobId>
     <Batch>Prod.Batch</Batch>
     <Start>2009-07-01T10:41:54</Start>
     <Slut>2009-12-04T07:42:40</Slut>
     <Rute>137</Rute>
     <Tankvogn>7037</Tankvogn>
     <SUM_Mængde>2</SUM_Mængde>
     <EquipmentId>A1_C1U02_Indvejning_2_råmælk</EquipmentId>
     <SLS>1</SLS>
     <PH>NA</PH>
    </Row>
</Rowset>

And here's my XSL:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:template match="/">
     <table border="1">
      <thead>
       <xsl:for-each select="Rowsets/Rowset/Columns/Column">
        <xsl:choose>
         <xsl:when test="substring(@Description, 1, 4) = 'SUM_'">
          <th><xsl:value-of select="substring(@Description,5)"/></th>
         </xsl:when>
         <xsl:otherwise>
          <th><xsl:value-of select="@Description"/></th>
         </xsl:otherwise>
        </xsl:choose>
       </xsl:for-each>
      </thead>
      <tbody>
       <xsl:for-each select="Rowsets/Rowset/Row">
        <tr>
        <xsl:for-each select="child::*">
         <td><xsl:value-of select="."/></td>
        </xsl:for-each>
        </tr>
       </xsl:for-each>
       <tr>
       <xsl:for-each select="Rowsets/Rowset/Columns/Column">    
        <td>
        <xsl:if test="substring(@Description, 1, 4) = 'SUM_'">
         Sum: <xsl:value-of select="sum(/Rowsets/Rowset/Row/@Description)"/>
        </xsl:if>
        </td>
       </xsl:for-each>
       </tr>
      </tbody>
     </table>
    </xsl:template>
</xsl:stylesheet>

Now, it's this piece of code that's giving me grey hairs:

<xsl:for-each select="Rowsets/Rowset/Columns/Column">      
        <td>
        <xsl:if test="substring(@Description, 1, 4) = 'SUM_'">
        Sum: <xsl:value-of select="sum(/Rowsets/Rowset/Row/@Description)"/>
        </xsl:if>
    </td>
</xsl:for-each>

I just cant seem to figure out how to make it sum over my SUM_xxx columns.. Hope someone can help me find a solution =) Until then, I'll have to hardcore the columns that needs to get summed..

+1  A: 

I can't test this at the moment, but try:

sum(/Rowsets/Rowset/Row/*[name()=current()/@Description])

That is the sum of the elements (any element hence the "*") where the name is equal to the description attribute of the current node.

Murph
I've tested this, and it works!
Tim C
Nice to know! Thanks
Murph
A: 

I think you want:

<xsl:if test="substring(@Description, 1, 4) = 'SUM_'">
  <xsl:text>Sum: </xsl:text>
  <xsl:value-of select="
    sum(/Rowsets/Rowset/Row/*[name() = current()/@SourceColumn])
  "/>
</xsl:if>
Tomalak
Looks to me like totals at the bottom of the columns of summable rows..
Murph
Yes, this occurred to me as well just now. ;-)
Tomalak
A: 

WHy is the if condition used??

bizz