首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xml/XSLT中的两个属性之和(一个循环中)

Xml/XSLT中的两个属性之和(一个循环中)
EN

Stack Overflow用户
提问于 2016-08-24 12:14:55
回答 3查看 265关注 0票数 0

到目前为止,我正在使用下面的代码来计算我的任务的ram使用量(在重新源标记中被命名为quantum ),但是在最后一部分,孩子的ram只会很好地显示出来,有人知道如何将它们相加吗?如果你能帮忙的话

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<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> 

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-08-24 13:15:07

如果您只能使用XSLT1.0,那么实现这一目标的另一种方法是使用递归模板来总结一组节点的修改后的quantum属性。

代码语言:javascript
复制
<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>

要想得到这笔钱,你可以这样做:

代码语言:javascript
复制
    <xsl:call-template name="resources">
        <xsl:with-param name="nodes" select="config/start/resource" />
    </xsl:call-template>

试试这个XSLT

代码语言:javascript
复制
<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>
票数 0
EN

Stack Overflow用户

发布于 2016-08-24 12:25:04

假设您可以使用XSLT2.0处理器

代码语言:javascript
复制
      <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')))"/>
票数 0
EN

Stack Overflow用户

发布于 2016-08-24 13:11:27

假设XSLT1.0处理器,请尝试:

代码语言:javascript
复制
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">

<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:variable name="quanta">
                                <xsl:for-each select="config/start">
                                    <q>
                                        <xsl:value-of select="substring-before(resource/@quantum,'M')"/>
                                    </q>
                                </xsl:for-each>
                            </xsl:variable>
                            <xsl:value-of select="sum(exsl:node-set($quanta)/q)"/>
                            <xsl:text>M</xsl:text>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39123049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档