2017-02-23 22 views
0

我有这样的XML和XSL例如:Addind值超过环路FOP与XSL和XML

XML:

<mastercount> 
    <sourceval> 
    <attm>50</attm> 
    <fh>6500</fh> 
    <id>1</id> 
    </sourceval> 
    <sourceval> 
    <attm>15</attm> 
    <fh>2300</fh> 
    <id>2</id> 
    </sourceval> 
    <sourceval> 
    <attm>4</attm> 
    <fh>280</fh> 
    <id>3</id> 
    </sourceval> 
    <sourceval> 
    <attm>20</attm> 
    <fh>2700</fh> 
    <id>4</id> 
    </sourceval> 
</mastercount> 

XSL:

<xsl:variable name="var_idx"> 
    <xsl:value-of select="position()" /> 
</xsl:variable> 
<xsl:variable name="var_sum_attm" /> 
<xsl:variable name="var_sum_fh" /> 
<fo:table-row> 
    <xsl:for-each select="/mastercount"> 
     <fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right"> 
      <fo:block> 
       <xsl:value-of select="sourceval[position()=$var_idx]/attm" />    
      </fo:block>  
     </fo:table-cell> 

     <fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right"> 
      <fo:block> 
       <xsl:value-of select="sourceval[position()=$var_idx]/fh" /> 
      </fo:block> 
     </fo:table-cell> 
    </xsl:for-each> 
</fo:table-row> 

源的这部分的伟大工程我得到

  attm fh attm fh 
Val2  50 6500 0 6500 
Val1  15 2300 0 2300 
Val3 280 0 280  0 
Val4  20 2700 0 2700 

(我跳过了不适用我以上部分源)

但是我现在需要的2场的总和:

  attm fh attm fh 
Val2  50 6500 0 6500 
Val1  15 2300 0 2300 
Val3 280 0 280  0 
Val4  20 2700 0 2700 
     sum sum sum  sum 

我怎样才能得到这个工作?

任何想法?

感谢您的帮助。 TheVagabond

UPDATE:

由于RT72这里是一个答案:

<xsl:for-each select="/mastercount"> 
    <fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right"> 
     <fo:block> 
     <xsl:value-of select="sum(sourceval/attm)"/>            
     </fo:block>          
    </fo:table-cell> 
    <fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right"> 
     <fo:block> 
     <xsl:value-of select="sum(sourceval/fh)" /> 
     </fo:block> 
    </fo:table-cell> 
</xsl:for-each> 

回答

1

以下应为你工作:

<xsl:variable name="var_sum_attm" select="sum(/mastercount/sourceval/attm)"/> 
<xsl:variable name="var_sum_fh" select="sum(/mastercount/sourceval/fh)"/> 

更新OP编辑问题

试试这个

<xsl:for-each select="/mastercount/sourceval"> 
<fo:table-row>        
    <fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right"> 
     <fo:block> 
      <xsl:value-of select="attm" /> 
     </fo:block> 
    </fo:table-cell> 
    <fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right"> 
     <fo:block> 
      <xsl:value-of select="fh" /> 
     </fo:block> 
    </fo:table-cell> 
</fo:table-row> 

<fo:table-row>        
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right"> 
    <fo:block> 
     <xsl:value-of select="sum(/mastercount/sourceval/attm)" /> 
    </fo:block> 
</fo:table-cell> 
<fo:table-cell padding="3pt" border-style="solid" width="12mm" border-width="1pt" text-align="right"> 
    <fo:block> 
     <xsl:value-of select="sum(/mastercount/sourceval/fh)" /> 
    </fo:block> 
</fo:table-cell> 
</fo:table-row> 
+1

看到我的编辑答案沿总和线(/ mastercount/sourceval/ATTM)。这会给你全部的总数 – RT72

+0

我没有显示总和出于某种原因,表字段存在并且正确,但是它们是空的 – Thevagabond

+1

您需要将变量声明放在您的for-each外 – RT72

0

,你也可以使用这个

<xsl:value-of select="sum(for $i in //attm return $i)"/> 
    <xsl:value-of select="sum(for $i in //fh return $i)"/> 
+0

我得到一个错误,,它预期,但被发现$?我如何获得$ I外循环,因为我需要在表后之行的每一行后不? – Thevagabond

+0

你可以从出表的调用这个价值,只是改变的XPath // ATTM到mastercount/sourceval/ATTM – Rupesh

1

它可以简单的使用xsl:template s表示反映您源的结构,让XSLT处理器工作了解要处理的内容。下面的例子确实在模板mastercount求和,并为sourceval模板和两个attmfh产生各自的农民组织:

<xsl:attribute-set name="table-cell"> 
    <xsl:attribute name="padding">3pt</xsl:attribute> 
    <xsl:attribute name="border-style">solid</xsl:attribute> 
    <xsl:attribute name="width">12mm</xsl:attribute> 
    <xsl:attribute name="border-width">1pt</xsl:attribute> 
    <xsl:attribute name="text-align">right</xsl:attribute> 
</xsl:attribute-set> 

<xsl:template match="mastercount"> 
    <fo:table> 
     <fo:table-body> 
      <xsl:apply-templates /> 
      <fo:table-row> 
       <fo:table-cell xsl:use-attribute-sets="table-cell"> 
        <fo:block> 
         <xsl:value-of select="sum(sourceval/attm)" /> 
        </fo:block> 
       </fo:table-cell> 
       <fo:table-cell xsl:use-attribute-sets="table-cell"> 
        <fo:block> 
         <xsl:value-of select="sum(sourceval/fh)" /> 
        </fo:block> 
       </fo:table-cell> 
      </fo:table-row> 
     </fo:table-body> 
    </fo:table> 
</xsl:template> 

<xsl:template match="sourceval"> 
    <fo:table-row> 
     <xsl:apply-templates select="attm | fh"/> 
    </fo:table-row> 
</xsl:template> 

<xsl:template match="attm | fh"> 
    <fo:table-cell xsl:use-attribute-sets="table-cell"> 
     <fo:block> 
      <xsl:apply-templates /> 
     </fo:block> 
    </fo:table-cell> 
</xsl:template> 

如果你想只生成与您的期望性质的fo:table-cell一个模板,你可以改变模板mastercountattmfh到:

<xsl:template match="mastercount"> 
    <fo:table> 
     <fo:table-body> 
      <xsl:apply-templates /> 
      <fo:table-row> 
       <xsl:call-template name="table-cell"> 
        <xsl:with-param name="value" select="sum(sourceval/attm)" /> 
       </xsl:call-template> 
       <xsl:call-template name="table-cell"> 
        <xsl:with-param name="value" select="sum(sourceval/fh)" /> 
       </xsl:call-template> 
      </fo:table-row> 
     </fo:table-body> 
    </fo:table> 
</xsl:template> 

<xsl:template match="attm | fh" name="table-cell"> 
    <xsl:param name="value" select="." /> 

    <fo:table-cell xsl:use-attribute-sets="table-cell"> 
     <fo:block> 
      <xsl:value-of select="$value" /> 
     </fo:block> 
    </fo:table-cell> 
</xsl:template>