Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 24th, 2009, 01:46 PM
Registered User
 
Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Converting an XML doc to XSLT

Hi all -

Need help in converting a flat XML doc to a nested XML structure using XSLT. I took a first pass at this. After this I am kind of stuck. Please let me know how to proceed further.

Source XML:

<data>
<row>
<id>200</id>
<level1>4000</level1>
<level2>26000</level2>
</row>
<row>
<id>200</id>
<level1>4000</level1>
<level2>16000</level2>
</row>
</data>

Output XML:
<data>

<node>
<id>200</id>
<level1>
<id>4000</id>
<level2>
<id>26000</id>
</level2>
<level2>
<id>16000</id>
</level2>
</level1>

</node>
</data>

And the XSLT I came up with that works for level1 only. Not sure how to get level2 though:

xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "2.0" >
<xsl:key name="records" match="row" use="id" />
<xsl:template match="data">
<data>
<!-- cycle through the first records in each group -->
<xsl:for-each select="row[generate-id() = generate-id(key('records',
id)[1])]">
<id><xsl:value-of select="id" /></id>
<!-- cycle through each of the records in the group -->
<xsl:for-each select="key('records', id)">
<level1><xsl:value-of select="level1" /></level1>
</xsl:for-each>
</xsl:for-each>
</data>
</xsl:template>

</xsl:stylesheet>

Any suggestions would be of great help

Thanks
K
 
Old July 24th, 2009, 02:00 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is an XSLT 2.0 solution working for two levels using for-each-group. If you have more levels then it makes sense to write a recursive function.
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  
  <xsl:output indent="yes"/>
  
  <xsl:template match="data">
    <xsl:copy>
      <xsl:for-each-group select="row" group-by="id">
        <node>
          <xsl:copy-of select="id"/>
          <xsl:for-each-group select="current-group()" group-by="level1">
            <level1>
              <id><xsl:value-of select="level1"/></id>
              <xsl:for-each-group select="current-group()" group-by="level2">
                <level2>
                  <id><xsl:value-of select="level2"/></id>
                </level2>
              </xsl:for-each-group>
            </level1>
          </xsl:for-each-group>
        </node>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old July 24th, 2009, 02:34 PM
Registered User
 
Join Date: Jul 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

wow, that's simple. Wondering what would be the efficiency of the transformation on a large input dataset. The dataset would typically be the output of a stored procedure which is going to be flat.

Just curious. Thanks
K
 
Old July 24th, 2009, 05:17 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The technique you used in your post is called Muenchian grouping, and it's the appropriate solution if you're stuck with an XSLT 1.0 processor. But I think you want to output the level1 outside the inner for-each, and the level2 inside it.

Martin's solution is much better if you've got XSLT 2.0 available.

As for efficiency, both solutions are likely to use indexes or hash tables internally and therefore be pretty scaleable.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Difficulties converting XML to XML using XSLT Reznik XSLT 7 June 3rd, 2008 05:45 AM
Creating XML doc ; writing string(xml format) into KamalRaturi XML 5 May 28th, 2008 05:51 AM
Converting XML into a particular format using XSLT AjayLuthria XSLT 1 April 10th, 2007 09:47 AM
Can 1 xslt transform an xml doc into 2 text files Raju Sarode XSLT 7 November 3rd, 2006 04:10 PM
Analyze XML doc using XSLT shumba XSLT 5 March 24th, 2006 05:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.