到目前为止,我正在使用下面的代码来计算我的任务的ram使用量(在重新源标记中被命名为quantum ),但是在最后一部分,孩子的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> 我很抱歉任何可能被搞砸的格式
发布于 2016-08-24 13:15:07
如果您只能使用XSLT1.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>发布于 2016-08-24 12:25:04
假设您可以使用XSLT2.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')))"/>发布于 2016-08-24 13:11:27
假设XSLT1.0处理器,请尝试:
<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>应用于输入示例,(呈现的)结果将是:

https://stackoverflow.com/questions/39123049
复制相似问题