全局变量使用并增加XSLT中的变量值

时间:2013-11-07 10:06:56

标签: xml xslt xslt-1.0 xslt-2.0

嗨,我搜索每一个我找不到任何答案或解决方案的地方,我需要这样做的方式;我的要求是我有这样的XML

 <mail>
     <hotel>
           <name>asd</name>
           <cost>50</cost>
     </hotel>
      <hotel>
           <name>sdesd</name>
           <cost>60</cost>
     </hotel>
    <totalcost>170</totalcost>
 </mail>

我有像这样的XSLT文件

<xsl:template match="/">
   <html>
      <xsl:for-each select="mail/hotel">
        <tr>HOTEL NAME : <xsl:valueof select="name"></tr>
        <tr>COST <xsl:valueof select="cost"></tr>
      </xsl:for-each>
        <tr>TOTAL COST WITH TAX <xsl:valueof select="mail/totalcost"></tr>
        <tr>TAX <b><xsl:valueof select="_____"></b></tr> (for this place I want to calculate cost values comeing under hotel child and set)   
   </html>
</xsl:template>

就像在java中一样,我们初始化globe变量并在for循环中设置增量明智值,然后使用最终出来的循环出来,我该如何在这个XSLT中做到这一点?

2 个答案:

答案 0 :(得分:2)

如果您想计算总和,请使用例如<xsl:value-of select="sum(mail/hotel/cost)"/>

答案 1 :(得分:0)

变量是xslt中的“值”,您只能设置一次。

对于常见操作,查找语言操作,例如sum()应该对你的例子做得很好。

如果您需要更复杂的东西,可以使用带变量的模板的递归调用。

E.g。

<xsl:template match="/" name="operation">
    <xsl:param name="value">0</xsl:param>
    <xsl:if test="$count < 100">
        <xsl:call-template name="operation">
            <xsl:with-param name="value" select="$value + 1"/>
        </xsl:call-template>
    </xsl:if>
    <xsl:if test="$count >= 100">
        <xsl:value-of select="$value"/>
    </xsl:if>
</xsl:template>