Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8931301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:05:05+00:00 2026-06-15T09:05:05+00:00

I got this input <?xml version=1.0 encoding=UTF-8?> <result> <datapoint poiid=2492 period=2004 value=1240/> <datapoint poiid=2492

  • 0

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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:05:06+00:00Added an answer on June 15, 2026 at 9:05 am

    Here is how to do it:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    
     <xsl:param name="k1" select="'poiid'"/>
     <xsl:param name="k2" select="'period'"/>
    
     <xsl:template match="result">
       <xsl:for-each-group select="datapoint" group-by="@*[name()= $k1]">
         <node type="{$k1}" id="{@*[name() = $k1]}">
           <xsl:for-each select="current-group()">
             <node type="{k2}" id="{@*[name()= $k2]}" value="{@value}"/>
           </xsl:for-each>
         </node>
       </xsl:for-each-group>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <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>
    

    the wanted, correct result is produced:

    <node type="poiid" id="2492">
       <node type="" id="2004" value="1240"/>
       <node type="" id="2005" value="1290"/>
       <node type="" id="2006" value="1280"/>
       <node type="" id="2007" value="1320"/>
       <node type="" id="2008" value="1330"/>
       <node type="" id="2009" value="1340"/>
       <node type="" id="2010" value="1340"/>
       <node type="" id="2011" value="1335"/>
    </node>
    <node type="poiid" id="2493">
       <node type="" id="2004" value="1120"/>
       <node type="" id="2005" value="1120"/>
       <node type="" id="2006" value="1100"/>
       <node type="" id="2007" value="1100"/>
       <node type="" id="2008" value="1100"/>
       <node type="" id="2009" value="1110"/>
       <node type="" id="2010" value="1105"/>
       <node type="" id="2011" value="1105"/>
    </node>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using this input XML: <?xml version=1.0 encoding=utf-8?> <Employees> <Employee ID=1> <FirstName>Klaus</FirstName> <LastName>Salchner</LastName> </Employee> <Employee
Trying to parse xml to text I got something like this, INPUT FILE <Item
I've basically got an XML input structure like this: ... <box type=rectangle class=someOriginalClass> <background
I got this: $('input[name=whatWeLove1]').before('<ul><li>'); $('input[name=whatWeLove1]').after('</li></ul>'); and it generates this: <ul> <li> </li> </ul> <input
I got this code in my submit form <form id=myform action='hello.php' method='GET'> <input type=button
I've got this form element: $form->input('ChecklistResponseGovernmentInfo.driversLicenseIsOnline', array('type'=>'radio', 'empty'=> true, 'options'=>array(0 => 'No', 1 =>
Hallo all. I got this piece of code: <div> <input id=id1 name=radioButton type=radio> <input
I've got a bash script that reads input from a file like this: while
in input XML I have a tag <name>Sample '</name> in XSL I transform this
Got this error message while trying to load view: The model item passed into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.