使用xsl:for-each-group从属性中选择和汇总值

时间:2019-02-20 20:42:17

标签: xml xslt-2.0

我正在尝试使用xsl:for-each-group来获得每种类型的每位用户的平均评分。

示例XML:

    <Ratings>
    <Rating name="Type1">
        <items userID="001" rating="5"/>
        <items userID="001" rating="3"/>
        <items userID="002" rating="2"/>
        <items userID="003" rating="5"/>
    </Rating>
    <Rating name="Type2">
        <items userID="002" rating="3"/>
        <items userID="003" rating="4"/>
        <items userID="003" rating="3"/>
    </Rating>
    <Rating name="Type3">
        <items userID="002" rating="5"/>
        <items userID="003" rating="5"/>
    </Rating>
</Ratings>

我已经深入到下面的XSLT(从通用复制模板开始,未显示)。

<xsl:template match="Rating">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:attribute name="averageRating">
            <xsl:value-of select="round(avg(items/@rating), 2)"/>
        </xsl:attribute>
        <xsl:for-each-group select="items" group-by="@userID">
            <userRating userID="{@userID}">
                <xsl:for-each select="current-group()">
                    <xsl:attribute name="averageRating">
                        <xsl:value-of select="round(avg(current()/@rating),2)"/>
                    </xsl:attribute>
                </xsl:for-each>
            </userRating>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

这使我(似乎是?)关闭了-每个用户被列出一次,并且他们的分组类型的等级显示在averageRating属性中。除了类型中某个用户有多个评分之外,只会显示最后一个评分,而不是该用户对该类型所有评分的平均值。

当前输出:

    <Ratings>
    <Rating name="Type1" averageRating="3.75">
      <userRating userID="001" averageRating="3"/>
      <userRating userID="002" averageRating="2"/>
      <userRating userID="003" averageRating="5"/>
   </Rating>
    <Rating name="Type2" averageRating="3.33">
      <userRating userID="002" averageRating="3"/>
      <userRating userID="003" averageRating="3"/>
   </Rating>
    <Rating name="Type3" averageRating="5">
      <userRating userID="002" averageRating="5"/>
      <userRating userID="003" averageRating="5"/>
   </Rating>
</Ratings>

所需的输出:

    <Ratings>
    <Rating name="Type1" averageRating="3.75">
      <userRating userID="001" averageRating="4"/> <!-- average of 5 and 3 -->
      <userRating userID="002" averageRating="2"/>
      <userRating userID="003" averageRating="5"/>
   </Rating>
    <Rating name="Type2" averageRating="3">
      <userRating userID="002" averageRating="3"/>
      <userRating userID="003" averageRating="3.5"/> <!-- average of 4 and 3 -->
   </Rating>
    <Rating name="Type3" averageRating="5">
      <userRating userID="002" averageRating="5"/>
      <userRating userID="003" averageRating="5"/>
   </Rating>
</Ratings>

当我将评级值添加为元素值(而不是作为属性)时,将显示所有评级,但显示为字符串,即计算仍然无法进行,例如:

      <userRating userID="001">53</userRating>

谢谢

1 个答案:

答案 0 :(得分:1)

代替

    <userRating userID="{@userID}">
            <xsl:for-each select="current-group()">
                <xsl:attribute name="averageRating">
                    <xsl:value-of select="round(avg(current()/@rating),2)"/>
                </xsl:attribute>
            </xsl:for-each>
        </userRating>

你只想要

<userRating userID="{@userID}" averageRating="{round(avg(current-group()/@rating),2)}">