1

I have an input xml

<document>   
      <row>   
        <column1>book1</column1>     
        <column2>00290</column2>      
        <column3>asddfr</column3>  
     </row>  
   <row>  
      <column1>book1</column1>  
      <column2>00290</column2>    
      <column3>cdcd</column3>  
    </row>  
     </document>

i want an output xml as , while traversing the whole xml I need grouping and sub-grouping for each of them

<book name="Book1"> 
    <title name="asddfr" code="00290"> 
        <title name="cdcd" code="00290"> 
 </book>
  • 1
    What have you tried so far? Have a look at this [SO post](http://stackoverflow.com/questions/2146648/how-to-apply-group-by-on-xslt-elements/2334224#2334224). – keenthinker Nov 03 '13 at 08:28
  • I am trying this using XSLT 2.0 and will use servingXml fro transformation,cince I don't know xslt I am having trouble doing this – user2949419 Nov 03 '13 at 08:37
  • If the XSLT you are looking for is supposed to create XML output then please post a well-formed XML result sample. Currently you have `title` start tags that are never closed so it is not clear which result you want. – Martin Honnen Nov 03 '13 at 10:00
  • 4
    "since I don't know xslt I am having trouble doing this". You can't write programs in a language you don't know. You might be lucky and get someone on this forum to write the program for you, but that's not what the forum is for. Get yourself a book on XSLT, work through some of the examples, read the section on grouping and xsl:for-each-group, try to solve this problem, and then come back here if you are still stuck. – Michael Kay Nov 03 '13 at 10:18
  • 1
    Take a look at http://www.xml.com/pub/a/2003/11/05/tr.html as that explains very well how grouping is done in XSLT 2.0. If you still can't quite get it to work, post what XSLT you have tried, and someone should be able to help you finish it off. Thanks! – Tim C Nov 03 '13 at 11:07

1 Answers1

0

This question is way too old to expect an answer. However for the benefit of others who may stumble on XSLT 2.0 grouping, below is the XSLT that will help along with the explanation.

The requirement is to group by book1 i.e. value in <column1> node for which XSLT 2.0 provides <xsl:for-each-group> feature.

<xsl:for-each-group select="row" group-by="column1" >

Once the grouping is done, node <book> can be created with an attribute @name having value stored in current-grouping-key(). Attribute value template i.e. curly braces are used to substitute the value returned.

<book name="{current-grouping-key()}">

Next step to loop within the current-group() to get the @name and @code for the <title> node.

<xsl:for-each select="current-group()">

The complete XSLT looks as below

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*"/>

    <xsl:template match="document">
        <xsl:for-each-group select="row" group-by="column1" >
            <book name="{current-grouping-key()}">
                <xsl:for-each select="current-group()">
                    <title name="{column3}" code="{column2}" />
                </xsl:for-each>
            </book>
        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

And the output is

<book name="book1">
   <title name="asddfr" code="00290"/>
   <title name="cdcd" code="00290"/>
</book>
Aniket V
  • 3,183
  • 2
  • 15
  • 27