Flat to Hierarchy XML

G

gsoftwares

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
 
J

Joris Gillis

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

gbhargav

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
 
J

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

gbhargav

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
 
J

Joris Gillis

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

gsoftwares

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
 
J

Joris Gillis

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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top