xslt sum节点值其中节点值

时间:2015-06-05 11:04:08

标签: xslt xslt-1.0

我有一个XSLT文件,它从XML文档中收集信息,然后将其呈现给CSV文件。

呈现所有信息的工作正常,但我现在需要做的是总结节点i的所有信息:totalDurationInSeconds其中i:issueBatchNumber = D12345

这是我到目前为止的代码

<xsl:template match="/">

<Textblock>Batch Number,Formula Code,Formula Name,Material Code,Material Name,Final Weight, Target Weight, Date, Material Weight, Job Name, Started At, Finished At</Textblock>
<xsl:value-of select="$newline"/>
<xsl:variable name ="BatchIds" select="//i:issues/i:issue"/>

<!-- begin for each loop -->
<xsl:for-each select="$BatchIds">
  <xsl:variable name="currentPosition" select="position()"/>
  <xsl:variable name="nextPosition" select="position()+1"/>
  <xsl:variable name="lastPosition" select="position()-1" />
  <xsl:variable name="lastBatchId" select="//i:issues/i:issue[$lastPosition]/i:issueBatchNumber"/>
  <xsl:variable name="thisBatchId" select="i:issueBatchNumber" />
  <xsl:choose>
    <xsl:when test="not($lastBatchId = $thisBatchId)" >
      <xsl:variable name="startingBatchId" select="//i:issues/i:issue[1]/i:issueBatchNumber"/>
      Batch ID: <xsl:value-of select="i:issueBatchNumber"/><xsl:value-of select="$newline"/>
      Total Time in Seconds: <sum value here -- this is the bit I dont know how to do>
    </xsl:when>
    <xsl:otherwise >

    </xsl:otherwise>
  </xsl:choose>
  outputting csv data here
</xsl:for-each>

这是我正在使用的xml数据的示例

<issues>
 <issue>
  <weight>0.903999984264374</weight>
  <materialBatch>
    <Code>WB821</materialCode>
    <weight>0</weight>
    <cost>0</cost>
  </materialBatch>
  <issueBatchNumber>D15601001</issueBatchNumber>
  <jobNumber>Default Job 2015-4</jobNumber>
  <date>2015-04-30T02:36:47</date>
  <dateStarted>2015-04-30T02:33:38</dateStarted>
  <finishedAt>2015-04-30T02:36:03</finishedAt>
  <dispenseDurationInSeconds>144.78</dispenseDurationInSeconds>
 </issue>
     <issue>
  <weight>0.903999984264374</weight>
  <materialBatch>
    <Code>WB821</materialCode>
    <weight>0</weight>
    <cost>0</cost>
  </materialBatch>
  <issueBatchNumber>D15601001</issueBatchNumber>
  <jobNumber>Default Job 2015-4</jobNumber>
  <date>2015-04-30T02:36:47</date>
  <dateStarted>2015-04-30T02:36:03</dateStarted>
  <finishedAt>2015-04-30T02:49.33</finishedAt>
  <dispenseDurationInSeconds>13.3</dispenseDurationInSeconds>
 </issue>
     <issue>
  <weight>0.903999984264374</weight>
  <materialBatch>
    <Code>WB821</materialCode>
    <weight>0</weight>
    <cost>0</cost>
  </materialBatch>
  <issueBatchNumber>D15601001</issueBatchNumber>
  <jobNumber>Default Job 2015-4</jobNumber>
  <date>2015-04-30T02:36:47</date>
  <dateStarted>2015-04-30T02:49.33</dateStarted>
  <finishedAt>2015-04-30T02:54.22</finishedAt>
  <dispenseDurationInSeconds>5.99</dispenseDurationInSeconds>
 </issue>
</issues>

1 个答案:

答案 0 :(得分:1)

您可以尝试这种方式:

<xsl:value-of 
    select="sum(../i:issue[i:issueBatchNumber=$thisBatchId]/i:dispenseDurationInSeconds)">

以上xpath返回来自父dispenseDurationInSeconds元素的所有issue的总和,其中子issueBatchNumber等于当前$thisBatchId值。