user3800792
user3800792

Reputation: 1

How to create a dynamic Group by in XSL (xsl:for-each-group) where group-by is defined by a variable

I would like to perform a Dynamic Grouping By based on a parameter available in my XML. When I try without varaviable it works but if I use a variable, I have no result. After a lot of researches I have not found a solution. Specialists can help me ? The data (XML) ... an extract

<dbquery id="KPI_SUMMARY"> 
    <descriptor> 
        <database>dbnode</database> 
        <originalquery> select something </originalquery> 
        <parameters> 
            <param value="STATUS" name="pR1"/> 
            <param value="PRIORITY" name="pR2"/> 
            <param value="YEAR" name="pC1"/> 
        </parameters> 
        <querystring> select something </querystring> 
    </descriptor> 
    <columns>
        <column name="NBR" type="BIGINT"/>
        <column name="YEAR" type="INTEGER"/>
        <column name="STATUS" type="VARCHAR"/>
        <column name="MISSION_CODE" type="VARCHAR"/>
        <column name="PRIORITY" type="VARCHAR"/>
    </columns>
    <rows> 
        <row PRIORITY="None" MISSION_CODE="No Mis" STATUS="H" YEAR="2006" NBR="2"/> 
        <row PRIORITY="None" MISSION_CODE="No Mis" STATUS="A" YEAR="2007" NBR="1"/> 
        <row PRIORITY="None" MISSION_CODE="No Mis" STATUS="H" YEAR="2007" NBR="2"/> 
        <row PRIORITY="None" MISSION_CODE="No Mis" STATUS="E" YEAR="2008" NBR="3"/> 
        <row PRIORITY="None" MISSION_CODE="No Mis" STATUS="A" YEAR="2009" NBR="28"/> 
        <row PRIORITY="None" MISSION_CODE="No Mis" STATUS="P" YEAR="2010" NBR="76"/> 
        <row PRIORITY="None" MISSION_CODE="No Mis" STATUS="D" YEAR="2011" NBR="114"/> 
        <row PRIORITY="None" MISSION_CODE="No Mis" STATUS="A" YEAR="2012" NBR="62"/> 
        <row PRIORITY="None" MISSION_CODE="No Mis" STATUS="D" YEAR="2013" NBR="5"/> 
        <row PRIORITY="None" MISSION_CODE="No Mis" STATUS="A" YEAR="2013" NBR="104"/> 
        <row PRIORITY="None" MISSION_CODE="No Mis" STATUS="B" YEAR="2014" NBR="4"/> 
        <row PRIORITY="None" MISSION_CODE="No Mis" STATUS="H" YEAR="2014" NBR="3"/> 
        <row PRIORITY="None" MISSION_CODE="No Mis" STATUS="P" YEAR="2014" NBR="331"/> 
        <row PRIORITY="None" MISSION_CODE="No Mis" STATUS="E" YEAR="2014" NBR="3"/> 
    </rows>
</dbquery> 

For info :

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>

Hard coded Transformation

    <xsl:for-each-group select = "//dbquery[@id='KPI_SUMMARY']/rows/row" group-by="@STATUS">
        <xsl:value-of select="current-grouping-key()"/> : <xsl:value-of select="sum(current-group()/@NBR)"/><br/>
    </xsl:for-each-group>

Result in html

H : 7
A : 195
E : 6
P : 407
D : 119
B : 4

Dynamic Transformation

    <xsl:variable name="VarGpBy">@<xsl:value-of select="//dbquery[1]/descriptor/parameters/param[@name='pR1']/@value"/></xsl:variable>
    Grouping Key : <xsl:value-of select='$VarGpBy'/><br/>
    <xsl:for-each-group select = "//dbquery[@id='KPI_SUMMARY']/rows/row" group-by="$VarGpBy">
        <xsl:value-of select="current-grouping-key()"/> : <xsl:value-of select="sum(current-group()/@NBR)"/><br/>
    </xsl:for-each-group>

Result in html

Grouping Key : @STATUS
@STATUS : 738

With the Dynamic approach, seems that the group By don't use the attribute @STATUS but the string @STATUS How can I solve this issue because the purpose is to create a template which can be called with a parameter for Grouping Selection

thx in advance

Upvotes: 0

Views: 483

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167696

Define the variable as <xsl:variable name="att-name" select="//dbquery[1]/descriptor/parameters/param[@name='pR1']/@value"/>, then use

<xsl:for-each-group select = "//dbquery[@id='KPI_SUMMARY']/rows/row" group-by="@*[local-name() = $att-name]">
    <xsl:value-of select="current-grouping-key()"/> : <xsl:value-of select="sum(current-group()/@NBR)"/><br/>
</xsl:for-each-group>

Upvotes: 1

Related Questions