Flat to Hierarchy XML

Discussion in 'XML' started by gsoftwares@gmail.com, Dec 10, 2004.

  1. Guest

    Hi XML Guru,

    I need to transform flat xml to Hierarchy xml. Can some one help me get
    started or have a same code with them

    my flat xml:


    <rowset>
    <row>
    <id>1</id>
    <parent_id>0</parent_id>
    </row>
    <row>
    <id>2</id>
    <parent_id>1</parent_id>
    </row>
    <row>
    <id>3</id>
    <parent_id>2</parent_id>
    </row>
    <row>
    <id>4</id>
    <parent_id>1</parent_id>
    </row>
    </rowset>

    I need in this hierarchy xml structure:

    <rowset>
    <row>
    <id>1</id>
    <parent_id>0</parent_id>
    <childs>
    <row>
    <id>2</id>
    <parent_id>1</parent_id>
    <childs>
    <row>
    <id>3</id>
    <parent_id>2</parent_id>
    </row>
    </childs>
    </row>
    </childs>
    </row>
    <row>
    <id>4</id>
    <parent>1</parent>
    </row>
    </rowset>


    please help me

    ~gbk
    , Dec 10, 2004
    #1
    1. Advertising

  2. Joris Gillis Guest

    > Hi XML Guru,
    >
    > I need to transform flat xml to Hierarchy xml. Can some one help me get
    > started or have a same code with them
    >

    Not that I'm to be cosidered a guru, but I'll try to help nonetheless...

    The following stylesheet will add the hierarchy structure like you asked, however not 100% indentical to your disired output (I'm somewhat confused about that last row nr 4 pending at the end in your sample)

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:eek:utput method="xml" indent="yes"/>

    <xsl:template match="rowset">
    <xsl:copy>
    <xsl:apply-templates select="row[parent_id=0]" mode="copy"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="row" mode="copy">
    <xsl:copy>
    <xsl:copy-of select="id"/>
    <xsl:copy-of select="parent_id"/>
    <xsl:apply-templates select="//row[parent_id=current()/id]"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="row">
    <childs>
    <xsl:apply-templates select="." mode="copy"/>
    </childs>
    </xsl:template>

    </xsl:stylesheet>

    Btw, did the xslt work for 'Level information for XML'? There was no response

    hope this helps...

    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
    Joris Gillis, Dec 10, 2004
    #2
    1. Advertising

  3. Guest

    Hi Joris Gillis,

    Thanks a lot, It works the way I wanted, it was my mistake about that
    4th record. Its really simple after i saw your response. Thanks for
    quick response.

    I figured out the level information ( My earlier post) by just adding
    <xsl:attribute name="node_level">
    <xsl:value-of select="count(ancestor::entity)"/>
    </xsl:attribute>

    ~gbk
    , Dec 10, 2004
    #3
  4. Joris Gillis Guest

    > Thanks a lot, It works the way I wanted, it was my mistake about that
    > 4th record. Its really simple after i saw your response. Thanks for
    > quick response.

    Ok, I'm glad it works. The code is indeed very simple if you look at it.
    > I figured out the level information ( My earlier post) by just adding
    > <xsl:attribute name="node_level">
    > <xsl:value-of select="count(ancestor::entity)"/>
    > </xsl:attribute>
    >


    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
    Joris Gillis, Dec 10, 2004
    #4
  5. Guest

    Hi Joris,

    One more question can I get result like this
    <rowset>
    <row id=1 parent_id=0>
    <childs>
    <row id=2 parent_id=1>
    <childs><row id=3 parent_id=2></row</childs>
    </row>
    <row id=4 parent_id=1></childs></row>
    </childs>
    </row>
    </rowset>

    Thanks again
    ~gbk
    , Dec 10, 2004
    #5
  6. Joris Gillis Guest

    > Hi Joris,
    >
    > One more question can I get result like this
    > <rowset>
    > <row id=1 parent_id=0>
    > <childs>
    > <row id=2 parent_id=1>
    > <childs><row id=3 parent_id=2></row</childs>
    > </row>
    > <row id=4 parent_id=1></childs></row>
    > </childs>
    > </row>
    > </rowset>


    Sure, no problem:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:eek:utput method="xml" indent="yes"/>

    <xsl:template match="rowset">
    <xsl:copy>
    <xsl:apply-templates select="row[parent_id=0]" mode="copy"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="row" mode="copy">
    <xsl:copy>
    <xsl:apply-templates/>
    <xsl:apply-templates select="//row[parent_id=current()/id]"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="row">
    <childs>
    <xsl:apply-templates select="." mode="copy"/>
    </childs>
    </xsl:template>

    <xsl:template match="row/*">
    <xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>
    </xsl:template>

    </xsl:stylesheet>


    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
    Joris Gillis, Dec 10, 2004
    #6
  7. Guest

    Hi Joris,

    I am posting message, but some how they are not showing in message
    board.
    Any ways thanks a lot both your solutions worked for me.

    For this one can i get result like this
    <rowset>
    <row id="1" parent_id="0">
    <childs>
    <row id="2" parent_id="1">
    <childs>
    <row id="3" parent_id="2">
    <childs/>
    </row>
    </childs>
    </row>
    <row id="4" parent_id="1">
    <childs/>
    </row>
    </childs>
    </row>
    </rowset>

    thanks
    ~gbk
    , Dec 10, 2004
    #7
  8. Joris Gillis Guest

    Hi again,

    > For this one can i get result like this
    > <rowset>
    > <row id="1" parent_id="0">
    > <childs>
    > <row id="2" parent_id="1">
    > <childs>
    > <row id="3" parent_id="2">
    > <childs/>

    If an empty 'childs' node is allowed, the code is much simplier (see below)
    > </row>
    > </childs>
    > </row>
    > <row id="4" parent_id="1">
    > <childs/>
    > </row>
    > </childs>
    > </row>
    > </rowset>
    >


    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:eek:utput method="xml" indent="yes"/>

    <xsl:template match="rowset">
    <xsl:copy>
    <xsl:apply-templates select="row[parent_id=0]"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="row">
    <xsl:copy>
    <xsl:apply-templates/>
    <childs>
    <xsl:apply-templates select="//row[parent_id=current()/id]"/>
    </childs>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="row/*">
    <xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>
    </xsl:template>

    </xsl:stylesheet>

    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
    Joris Gillis, Dec 10, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Homer
    Replies:
    230
    Views:
    10,689
    Roedy Green
    May 1, 2006
  2. BlackSnail
    Replies:
    1
    Views:
    416
    Christopher Boomer
    Aug 6, 2003
  3. hakhan
    Replies:
    0
    Views:
    411
    hakhan
    Oct 19, 2004
  4. charles
    Replies:
    4
    Views:
    704
    Johannes Busse
    Dec 7, 2004
  5. Replies:
    0
    Views:
    558
Loading...

Share This Page