Xml / XSLT中2个属性的总和(在循环中)

时间:2016-08-24 12:14:56

标签: xml xslt

我正在尝试计算我的任务的ram消息(在ressource标签中命名为量子)到目前为止我使用的是下面的代码,但在最后一部分,孩子们的ram只能用于显示,有人有任何想法关于如何将它们加在一起?和帮助将不胜感激

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
<body>
   <h2>Ram Usage Overview</h2>
  <table border="1">
    <tr>
      <th>Task</th>
      <th>Parent</th>
      <th>Path</th>
      <th>Ram(self)</th>
      <th>Ram(Children)</th>
    </tr>
    <xsl:for-each select="//start">
    <tr>
      <td>
        <xsl:value-of select="@name"/>
      </td>
      <td>
        <xsl:value-of select="ancestor::start[1]/@name"/>
      </td>
      <td>
        <xsl:for-each select="ancestor-or-self::start">
          <xsl:value-of select="@name"/>
          <xsl:if test="position() != last()">
            <xsl:text> -> </xsl:text>
          </xsl:if>
        </xsl:for-each>
      </td>
      <td>
        <xsl:value-of select="resource/@quantum"/>
      </td>
      <td>
          <xsl:for-each select="config/start">
            <xsl:value-of select="substring-before(resource/@quantum,'M')"/>
<!--this is where i want get the sum-->
          </xsl:for-each>
      </td>
    </tr>
    </xsl:for-each>
  </table>
</body>
 </html>
</xsl:template>

</xsl:stylesheet>

XML          

<config name="init">
    <start name="A">
        <resource name="RAM" quantum="2024M"/>
        <config>
            <start name="B">
                <resource name="RAM" quantum="1024M"/>
            </start>
            <start name="C">
                <resource name="RAM" quantum="1024M"/>
                <config>
                    <start name="D">
                        <resource name="RAM" quantum="512M"/>
                    </start>
                    <start name="E">
                        <resource name="RAM" quantum="512M"/>
                        <config>
                            <start name="F">
                                <resource name="RAM" quantum="256M"/>
                                <config>
                                    <start name="H">
                                        <resource name="RAM" quantum="128M"/>
                                    </start>
                                    <start name="I">
                                        <resource name="RAM" quantum="128M"/>
                                    </start>
                                </config>
                            </start>
                            <start name="G">
                                <resource name="RAM" quantum="256M"/>
                            </start>
                        </config>
                    </start>
                </config>
            </start>
        </config>
    </start>
</config> 

我很抱歉任何可能搞砸的格式

3 个答案:

答案 0 :(得分:0)

假设您可以使用XSLT 2.0处理器

      <xsl:for-each select="config/start">
        <xsl:value-of select="substring-before(resource/@quantum,'M')"/>
      </xsl:for-each>
      <!-- compute sum -->
      <xsl:value-of select="sum(config/start/resource/@quantum/number(substring-before(., 'M')))"/>

答案 1 :(得分:0)

假设 XSLT 1.0 处理器,请尝试:

name , age ,gender

应用于您的输入示例,(呈现)结果将为:

SQLite

答案 2 :(得分:0)

如果你只能使用XSLT 1.0,那么另一种方法就是使用递归模板来总结一组节点的修改后的quantum属性。

<xsl:template name="resources">
    <xsl:param name="sum" select="0" />
    <xsl:param name="nodes" />

    <xsl:choose>
        <xsl:when test="$nodes">
            <xsl:call-template name="resources">
                <xsl:with-param name="sum" select="$sum + number(substring-before($nodes[1]/@quantum,'M'))" />
                <xsl:with-param name="nodes" select="$nodes[position() > 1]" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise><xsl:value-of select="$sum" /></xsl:otherwise>
    </xsl:choose>
</xsl:template>

要调用它来产生总和,你可以这样做:

    <xsl:call-template name="resources">
        <xsl:with-param name="nodes" select="config/start/resource" />
    </xsl:call-template>

试试这个XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
<body>
   <h2>Ram Usage Overview</h2>
  <table border="1">
    <tr>
      <th>Task</th>
      <th>Parent</th>
      <th>Path</th>
      <th>Ram(self)</th>
      <th>Ram(Children)</th>
    </tr>
    <xsl:for-each select="//start">
    <tr>
      <td>
        <xsl:value-of select="@name"/>
      </td>
      <td>
        <xsl:value-of select="ancestor::start[1]/@name"/>
      </td>
      <td>
        <xsl:for-each select="ancestor-or-self::start">
          <xsl:value-of select="@name"/>
          <xsl:if test="position() != last()">
            <xsl:text> -> </xsl:text>
          </xsl:if>
        </xsl:for-each>
      </td>
      <td>
        <xsl:value-of select="resource/@quantum"/>
      </td>
      <td>
        <xsl:call-template name="resources">
            <xsl:with-param name="nodes" select="config/start/resource" />
        </xsl:call-template>
      </td>
    </tr>
    </xsl:for-each>
  </table>
</body>
 </html>
</xsl:template>

<xsl:template name="resources">
    <xsl:param name="sum" select="0" />
    <xsl:param name="nodes" />
    <xsl:choose>
        <xsl:when test="$nodes">
            <xsl:call-template name="resources">
                <xsl:with-param name="sum" select="$sum + number(substring-before($nodes[1]/@quantum,'M'))" />
                <xsl:with-param name="nodes" select="$nodes[position() > 1]" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise><xsl:value-of select="$sum" /></xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>