473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Flat to Hierarchy XML

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

Jul 20 '05 #1
7 5378
> 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:output 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-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #2
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

Jul 20 '05 #3
> 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-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #4
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

Jul 20 '05 #5
> 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:output 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-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #6
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

Jul 20 '05 #7
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:output 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-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Nicolas Girard | last post by:
Hi, Forgive me if the answer is trivial, but could you tell me how to achieve the following: {k1:,k2:v3,...} --> ,,,...] The subtle point (at least to me) is to "flatten" values that are...
13
by: raykyoto | last post by:
Hi all, I'm sure this is a popular question that comes up every few months here. Indeed, I've looked at some of the past postings, but I would like to ask things differently. Basically, I'm...
22
by: Daniel Billingsley | last post by:
Ok, I wanted to ask this separate from nospam's ridiculous thread in hopes it could get some honest attention. VB6 had a some simple and fast mechanisms for retrieving values from basic text...
1
by: Gustaf Liljegren | last post by:
My input data consists of a list of parent-child relationships. One item in this list can be modelled like this: +---------+ | Item | +---------+ | Parent | | Child | +---------+
0
by: sugarsmack | last post by:
Hi folks, Using XSL, I'm trying to take an XML file containing a flat list of "topics" and generate a hierarchical topic map. The topic nodes include a role attribute that indicates their position...
9
by: FFMG | last post by:
In my site I have a config table, (MySQL), with about 30 entries; the data is loaded on every single page load. This is not the only call to the db, (we do a total of about 8 calls to the db). As...
15
by: Richard | last post by:
Can anyone recommend a good online resource listing all C keywords, standard system calls, defines etc in a "flat" hierarchy. I wish to set up some bindings in order to bring up "hints and tips"...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
1
by: doozer1979 | last post by:
Hello, I Have a flat XML file that i have exported from MS access. When i export it from access i want to run it through an xslt to turn it into a hierachical structure Each person has a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.