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 November 22nd, 2006, 01:16 AM
kkt kkt is offline
Registered User
 
Join Date: Nov 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default transform from flat to hierarchical

Hi all,

I need to transform the following flat and somewhat unstructured sample XML content:

<logs>
  <log>a</log>
  <log>sub b</log>
  <log>c</log>
  <log>sub d</log>
  <log>e</log>
  <log>f</log>
  <log>end of d</log>
  <log>g</log>
  <log>end of b</log>
  <log>h</log>
  <log>i</log>
<logs>

into a more structured content with nesting:

<logs>
  <log name="a">
    <log name="b">
      <log name="c">
        <log name="d">
          <log name="e"/>
          <log name="f"/>
        </log>
      </log>
      <log name="g"/>
    </log>
  </log>
  <log name="h"/>
  <log name="i"/>
</logs>

Is this doable by using XSLT/XPATH?.

Thanks,
kkt

 
Old November 23rd, 2006, 02:13 AM
Authorized User
 
Join Date: Dec 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ramesh.kumarm
Default

kkt,
This is possible.
You need to just use the xsl:value-of and xsl:attribute to do this.
As the tag names are similar...you could possibly use the tag names hierarchially with (1),(2)...etc to fetch the values.
For retrieving a character from a string...try to use funtions like string-before or string-after or a regex patern match - to get what you want.

thanx

 
Old November 23rd, 2006, 03:26 PM
kkt kkt is offline
Registered User
 
Join Date: Nov 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply. Do you have some sample XSLT code to perform such a transformation?

Thanks,
kkt

 
Old November 24th, 2006, 05:48 AM
Authorized User
 
Join Date: Dec 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ramesh.kumarm
Default

Check this out!
Got time to kick this off!!

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="xsl fo">
    <xsl:template match="/">
        <logs>
            <log>
                <xsl:attribute name="name"><xsl:value-of select="//*/log[1]"/></xsl:attribute>
                <log>
                    <xsl:attribute name="name">
                        <xsl:variable name="x" select="//*/log[2]"/>
                        <xsl:value-of select="substring-after($x,' ')"/>
                    </xsl:attribute>
                    <log>
                        <xsl:attribute name="name"><xsl:value-of select="//*/log[3]"/></xsl:attribute>
                        <log>
                            <xsl:attribute name="name">
                                <xsl:variable name="x" select="//*/log[4]"/>
                                <xsl:value-of select="substring-after($x,' ')"/>
                            </xsl:attribute>
                            <log>
                            <xsl:attribute name="name"><xsl:value-of select="//*/log[5]"/></xsl:attribute>
                            </log>
                            <log>
                            <xsl:attribute name="name"><xsl:value-of select="//*/log[6]"/></xsl:attribute>
                            </log>
                        </log>
                    </log>
                    <log>
                    <xsl:attribute name="name"><xsl:value-of select="//*/log"/></xsl:attribute>
                    </log>
                </log>
            </log>
            <log>
            <xsl:attribute name="name"><xsl:value-of select="//*/log[10]"/></xsl:attribute>
            </log>
            <log>
            <xsl:attribute name="name"><xsl:value-of select="//*/log[11]"/></xsl:attribute>
            </log>
        </logs>
    </xsl:template>
</xsl:stylesheet>

 
Old November 24th, 2006, 12:36 PM
kkt kkt is offline
Registered User
 
Join Date: Nov 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again for your post.

The XML content in my previous post is just an example. I need a more flexible solution to handle different nestings.

Thanks,
kkt

 
Old November 27th, 2006, 02:15 AM
Authorized User
 
Join Date: Dec 2005
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ramesh.kumarm
Default

Then just have it inside a loop. Thats it.

Ramesh
"Always Look For Something NEW"





Similar Threads
Thread Thread Starter Forum Replies Last Post
flat vs. hierarchical style asker XML 0 March 28th, 2007 06:16 AM
flat XML to hierarchical XML eduijs XSLT 1 April 28th, 2006 05:43 AM
From flat to hierarchical seesharper XSLT 2 February 5th, 2006 06:48 AM
translating a flat xml to a hierarchical xml stevea XSLT 4 June 13th, 2005 05:55 PM
How to make hierarchical data "lay flat" using SQL tinlong SQL Language 1 August 1st, 2003 01:38 AM





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