XML块的条件计数

时间:2013-02-20 10:38:55

标签: xml xslt counter

我有一个XML文件,格式如下:

<block>
 <name> blocky </name>
 <time1> 20 </time1>
 <time2> 17 </time2>
</block>
<block>
 <name> rocky </name>
 <time1> 3 </time1>
 <time2> 5 </time2>
</block>
...

就此而言,我将定义“差异”。一个块的值是time1的值,div是time2的值。

我需要做的是计算具有差异的块的数量&gt; 10%,并且还找到具有最大差异的块并打印它的名称。

如何使用XSLT完成此操作?我无法更新变量,因此无法使用计数器,也无法使用最大值。

感谢您的帮助

1 个答案:

答案 0 :(得分:4)

我假设所有的块都是&lt; blocks&gt;的子元素。元件。

您的问题的解决方案是:

<xsl:template match="blocks">
    <!-- Count number of blocks with diff greater than a number, in this case 10,
         because I do not know which is the maximum value (100%) that you can
         obtain with your specific data -->
    <xsl:value-of select="count(block[number(time1) div number(time2) >= 10])" />
    <!-- Find a block with maximum diff and print its name -->
    <xsl:for-each select="block">
        <xsl:sort select="number(time1) div number(time2)" data-type="number" order="descending" />
        <xsl:if test="position() = 1">
            <xsl:value-of select="name" />
        </xsl:if>
    </xsl:for-each>
</xsl:template>