Reputation: 118
my xml looks like next:
<root>
<r time="2" user="loginIP=100&loginName=a0" />
<r time="3" user="loginIP=101&loginName=a2" />
<r time="4" user="loginIP=102&loginName=a3" />
<r time="6" user="loginIP=101&loginName=a2" />
<r time="5" user="loginIP=104&loginName=a5" />
<r time="8" user="loginIP=105&loginName=a7" />
<r time="9" user="loginIP=105&loginName=a7" />
<r time="11" user="loginIP=104&loginName=a5" />
</root>
Q: now I want to get total time of each user (loginName stands for the user), then show which user's total time is minimum, and which user's total time is max.
I tried to use: //r[not(substring-after(./@user,"loginName=")=substring-after(following-sibling::r/@user,"loginName="))] to get the all user name first, but it failed, this xpath will return duplicate value.
so how can I achieve my goal?
Upvotes: 0
Views: 92
Reputation: 167571
The XSLT 1.0 stylesheet
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:key name="user" match="root/r" use="substring-after(substring-after(@user, '='), '=')"/>
<xsl:template match="root">
<xsl:apply-templates select="r[generate-id() = generate-id(key('user', substring-after(substring-after(@user, '='), '='))[1])]">
<xsl:sort select="sum(key('user', substring-after(substring-after(@user, '='), '='))/@time)" data-type="number"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="root/r">
<xsl:value-of select="concat('User: ', substring-after(substring-after(@user, '='), '='), ': ', sum(key('user', substring-after(substring-after(@user, '='), '='))/@time))"/>
<xsl:if test="position() = 1">
<xsl:text> (minimum)</xsl:text>
</xsl:if>
<xsl:if test="position() = last()">
<xsl:text> (maximum)</xsl:text>
</xsl:if>
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
uses Muenchian grouping to group by the user and then sorts by the sum
of the @time
values in each group to output the sorted values, indicating the smallest and largest value.
So for your sample the ouput is
User: a0: 2 (minimum)
User: a3: 4
User: a2: 9
User: a5: 16
User: a7: 17 (maximum)
Upvotes: 1