XSLT 1.0:分组和选择计算min的时间

时间:2012-03-09 19:01:34

标签: xslt xpath

我正在尝试从XML计算一组JMeter结果的最小和最大时间,如下所示,但是当我使用../httpSample[@lb = current()/@lb]/@t调用我的min模板时,它无法正确计算时间。

<httpSample t="758" lt="0" ts="1330176857546" s="false" lb="/app1/" tn="space Guest Users 2-4" dt="text" by="1446"/>
<httpSample t="213" lt="0" ts="1330176858088" s="false" lb="/app2/" tn="space Logged In Users 1-28" dt="text" by="1446"/>
<httpSample t="153" lt="0" ts="1330176858088" s="false" lb="/app2/" tn="space Logged In Users 1-28" dt="text" by="1446"/>
<httpSample t="113" lt="0" ts="1330176858088" s="false" lb="/app2/" tn="space Logged In Users 1-28" dt="text" by="1446"/>
<httpSample t="153" lt="0" ts="1330176858149" s="false" lb="/app3/" tn="space Logged In Users 1-29" dt="text" by="1446"/>
<httpSample t="340" lt="0" ts="1330176857967" s="false" lb="/app3/" tn="space Logged In Users 1-26" dt="text" by="1446"/>

xsl:message ../httpSample[@lb = current()/@lb]中的表达式在分组方面正常工作,但它没有正确选择时间属性。

非常感谢提前。

<xsl:for-each select="/testResults/httpSample[not(@lb = preceding::*/@lb)]">

            <xsl:variable name="lab" select="@lb" />
            <xsl:variable name="count" select="count(../httpSample[@lb = current()/@lb])" />
            <xsl:variable name="failureCount" select="count(../httpSample[@lb = current()/@lb][attribute::s='false'])" />
            <xsl:variable name="successCount" select="count(../httpSample[@lb = current()/@lb][attribute::s='true'])" />
            <xsl:variable name="successPercent" select="$successCount div $count" />
            <xsl:variable name="totalTime" select="sum(../httpSample[@lb = current()/@lb]/@t)" />
            <xsl:variable name="averageTime" select="$totalTime div $count" />

            <xsl:message>
                times: <xsl:copy-of select="../httpSample[@lb = current()/@lb]/@t"/>
            </xsl:message>

            <xsl:variable name="minTime">
                <xsl:call-template name="min">
                    <xsl:with-param name="nodes" select="../httpSample[@lb = current()/@lb]/@t" />
                </xsl:call-template>
            </xsl:variable>

...

<xsl:template name="min">
    <xsl:param name="nodes" select="/.." />

    <!-- Broken when we get here -->
    <xsl:choose>
        <xsl:when test="not($nodes)">NaN</xsl:when>
        <xsl:otherwise>
            <xsl:for-each select="$nodes">
                <xsl:sort data-type="number" />
                <xsl:if test="position() = 1">
                    <xsl:value-of select="number(.)" />
                </xsl:if>
            </xsl:for-each>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

2 个答案:

答案 0 :(得分:0)

我建议你进行不同的分组。下面是一个XSLT 2.0解决方案 - 如果你想要一个XSLT 1.0,请告诉我。增强以下内容应该不难。

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <output>
            <xsl:for-each-group select="root/httpSample" group-by="@lb">
                <httpSampleGroup>
                    <lb><xsl:value-of select="current-grouping-key()"/></lb>
                    <minT><xsl:value-of select="min(current-group()/@t)"/></minT>
                </httpSampleGroup>
            </xsl:for-each-group>
        </output>
    </xsl:template>
</xsl:stylesheet>

使用您的输入(加上根节点),这将给出

<?xml version="1.0" encoding="UTF-8"?>
<output>
    <httpSampleGroup>
        <lb>/app1/</lb>
        <minT>758</minT>
    </httpSampleGroup>
    <httpSampleGroup>
        <lb>/app2/</lb>
        <minT>113</minT>
    </httpSampleGroup>
    <httpSampleGroup>
        <lb>/app3/</lb>
        <minT>153</minT>
    </httpSampleGroup>
</output>

答案 1 :(得分:0)

我稍微触及了您的代码,现在按预期工作

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
    <xsl:for-each select=
    "/testResults/httpSample[not(@lb = preceding::*/@lb)]">

                <xsl:variable name="lab" select="@lb" />
                <xsl:variable name="count" select="count(../httpSample[@lb = current()/@lb])" />
                <xsl:variable name="failureCount" select="count(../httpSample[@lb = current()/@lb][attribute::s='false'])" />
                <xsl:variable name="successCount" select="count(../httpSample[@lb = current()/@lb][attribute::s='true'])" />
                <xsl:variable name="successPercent" select="$successCount div $count" />
                <xsl:variable name="totalTime" select="sum(../httpSample[@lb = current()/@lb]/@t)" />
                <xsl:variable name="averageTime" select="$totalTime div $count" />

                <xsl:message>
                    times:
                     <xsl:for-each select="../httpSample[@lb = current()/@lb]">
                       <xsl:if test="not(position() = 1)">,</xsl:if>
                       <xsl:value-of select="@t"/>
                     </xsl:for-each>
                </xsl:message>

                <xsl:variable name="minTime">
                    <xsl:call-template name="min">
                        <xsl:with-param name="nodes" select="../httpSample[@lb = current()/@lb]/@t" />
                    </xsl:call-template>
                </xsl:variable>

                Min time: <xsl:value-of select="$minTime"/>
   </xsl:for-each>
 </xsl:template>

     <xsl:template name="min">
        <xsl:param name="nodes" select="/.." />

        <!-- Broken when we get here -->
        <xsl:choose>
            <xsl:when test="not($nodes)">NaN</xsl:when>
            <xsl:otherwise>
                <xsl:for-each select="$nodes">
                    <xsl:sort data-type="number" />
                    <xsl:if test="position() = 1">
                        <xsl:value-of select="number(.)" />
                    </xsl:if>
                </xsl:for-each>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

在提供的XML文档上应用此转换时

<testResults>
    <httpSample t="758" lt="0" ts="1330176857546"
    s="false" lb="/app1/"
    tn="space Guest Users 2-4" dt="text"
    by="1446"/>
    <httpSample t="213" lt="0" ts="1330176858088"
    s="false" lb="/app2/"
    tn="space Logged In Users 1-28" dt="text"
    by="1446"/>
    <httpSample t="153" lt="0" ts="1330176858088"
    s="false" lb="/app2/"
    tn="space Logged In Users 1-28" dt="text"
    by="1446"/>
    <httpSample t="113" lt="0" ts="1330176858088"
    s="false" lb="/app2/"
    tn="space Logged In Users 1-28" dt="text"
    by="1446"/>
    <httpSample t="153" lt="0" ts="1330176858149"
    s="false" lb="/app3/"
    tn="space Logged In Users 1-29" dt="text"
    by="1446"/>
    <httpSample t="340" lt="0" ts="1330176857967"
    s="false" lb="/app3/"
    tn="space Logged In Users 1-26" dt="text"
    by="1446"/>
</testResults>

生成所需的正确结果(包括调试输出):

            Min time: 758

            Min time: 113

            Min time: 153