XSLT for-each-group group-by变量

时间:2015-06-03 07:01:11

标签: xml xslt group-by

我正在尝试为我的XSLT实现更灵活的groub-by语句,使用变量(取决于变量值,需要不同的分组):

.......
    <xsl:param name="rows"/>
    <xsl:variable name="groupBy">
        <xsl:choose>
            <xsl:when test="$report_type='first_type'">
                <xsl:value-of select="concat(date,ccy)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat(type,ccy)"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:for-each-group select="$rows" group-by="$groupBy">
    .....
    </xsl:for-each-group>

据我所知,这种结构不起作用(好吧,它对我没用)。

我的问题是:如何将变量传递给group-by属性?

提前感谢您提供任何帮助。

2 个答案:

答案 0 :(得分:3)

这里的问题不是“如何将变量传递给group-by属性”,而是传递的变量的内容是什么。 group-by属性必须包含XPath 表达式 - 您传递的是字符串值。

如果只有两种方法对节点进行分组,我建议您使用以下构造:

<xsl:for-each-group select="item" group-by="if($report_type='first_type') then concat(date,ccy) else concat(type,ccy)">

答案 1 :(得分:3)

是的,group-by表达式可以是变量引用,但是它对于总体中的每个项都具有相同的值,因此这是非常没有意义的。这有点像分组来说&#34; 42&#34;。

一般情况下,如果分组键的计算过于复杂而无法在group-by属性中内联,则最佳解决方案是将其置于函数中,然后可以将其称为group-by =&#34;我的:(。)。分组密钥&#34;

我不相信这里的情况。你可以写这个,例如,

group-by="concat(if ($report_type='first_type') then date else type, ccy)"
相关问题