Dynamic parameters for XSLT 2.0 group-by

Posted by Ophileon on Stack Overflow See other posts from Stack Overflow or by Ophileon
Published on 2012-12-02T19:56:11Z Indexed on 2012/12/02 23:04 UTC
Read the original article Hit count: 196

Filed under:
|
|
|

I got this input

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <datapoint poiid="2492" period="2004" value="1240"/>
  <datapoint poiid="2492" period="2005" value="1290"/>
  <datapoint poiid="2492" period="2006" value="1280"/>
  <datapoint poiid="2492" period="2007" value="1320"/>
  <datapoint poiid="2492" period="2008" value="1330"/>
  <datapoint poiid="2492" period="2009" value="1340"/>
  <datapoint poiid="2492" period="2010" value="1340"/>
  <datapoint poiid="2492" period="2011" value="1335"/>
  <datapoint poiid="2493" period="2004" value="1120"/>
  <datapoint poiid="2493" period="2005" value="1120"/>
  <datapoint poiid="2493" period="2006" value="1100"/>
  <datapoint poiid="2493" period="2007" value="1100"/>
  <datapoint poiid="2493" period="2008" value="1100"/>
  <datapoint poiid="2493" period="2009" value="1110"/>
  <datapoint poiid="2493" period="2010" value="1105"/>
  <datapoint poiid="2493" period="2011" value="1105"/>
</result>

and I use this xslt 2.0

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">


    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="result">

        <xsl:for-each-group select="datapoint" group-by="@poiid">
            <node type="poiid" id="{@poiid}">
                <xsl:for-each select="current-group()">
                    <node type="period" id="{@period}" value="{@value}"/>
                </xsl:for-each>
            </node>

        </xsl:for-each-group>

    </xsl:template>

</xsl:stylesheet>

to convert it into

<?xml version="1.0" encoding="UTF-8"?>
<node type="poiid" id="2492">
   <node type="period" id="2004" value="1240"/>
   <node type="period" id="2005" value="1290"/>
   <node type="period" id="2006" value="1280"/>
   <node type="period" id="2007" value="1320"/>
   <node type="period" id="2008" value="1330"/>
   <node type="period" id="2009" value="1340"/>
   <node type="period" id="2010" value="1340"/>
   <node type="period" id="2011" value="1335"/>
</node>
<node type="poiid" id="2493">
   <node type="period" id="2004" value="1120"/>
   <node type="period" id="2005" value="1120"/>
   <node type="period" id="2006" value="1100"/>
   <node type="period" id="2007" value="1100"/>
   <node type="period" id="2008" value="1100"/>
   <node type="period" id="2009" value="1110"/>
   <node type="period" id="2010" value="1105"/>
   <node type="period" id="2011" value="1105"/>
</node>

Works smoothly.

Where I got stuck is when I tried to make it more dynamic. The real life input has 6 attributes for each datapoint instead of 3, and the usecase requires the possibility to set the grouping parameters dynamically.

I tried using parameters

    <xsl:param name="k1" select="'poiid'"/>
    <xsl:param name="k2" select="'period'"/>

but passing them to the rest of the xslt is something that I can't get right. The code below doesn't work, but clarifies hopefully, what I'm looking for.

    <xsl:template match="result">

        <xsl:for-each-group select="datapoint" group-by="@{$k1}">
            <node type="{$k1}" id="@{$k1}">
                <xsl:for-each select="current-group()">
                    <node type="{$k2}" id="@{$k2}" value="{@value}"/>
                </xsl:for-each>
            </node>

        </xsl:for-each-group>

    </xsl:template>

Any help appreciated..

© Stack Overflow or respective owner

Related posts about dynamic

Related posts about group-by