XSLT for-each-group group-by 变量

XSLT for-each-group group-by variable

我正在尝试为我的 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属性?

提前感谢您的帮助。

这里的问题不是“如何将变量传递给group-by属性”,而是内容是什么传递的变量。 group-by 属性必须包含一个 XPath expression - 您正在向它传递一个 string 值。

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

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

是的,group-by 表达式可以是一个变量引用,但它对总体中的每个项目都具有相同的值,所以这是毫无意义的。这有点像说 group-by="42".

一般来说,如果分组键的计算过于复杂,无法在 group-by 属性中内联,最好的解决方案是将其放在一个函数中,然后您可以调用 group-by="my:grouping-key(.)".

我不相信这里是这种情况。例如,您可以将其写为

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