Je suis un peu novice en ce qui concerne XSLT et XPath, donc j'ai rencontré ce problème. Je crée une table simple basée sur une vue sur la base de données, qui doit résumer certaines des colonnes de la vue. Ce que je pouvais faire était d'ajouter 'SUM_' comme préfixe de tous les couples que je veux additionner, et couper ce tag avec une sous-chaîne quand le nom de coulmn devrait être montré.Contenu de la colonne basé sur une variable avec XPath
est ici en exemple de mon 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>
Et voici mon XSL:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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>
Maintenant, il est ce morceau de code qui me donne des cheveux gris:
<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>
Je ne peux pas sembler comprendre comment faire la somme de mes colonnes SUM_xxx .. Hope someon e peut me aider à trouver une solution =) Jusque-là, je vais devoir hardcore les colonnes qui doit se résumer ..
Je l'ai testé, et il fonctionne! –
Bon à savoir! Merci – Murph