Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Flat to hierarchical using attributes

Reply
Thread Tools

Flat to hierarchical using attributes

 
 
Steve Mac
Guest
Posts: n/a
 
      12-30-2004
I am very new to XML / XSL and am having trouble with transforming a
flat xml file into a hierarchical format using an id / parentid
attribute relationship. Any help on this would be greatly appreciated.

I have an XML document in the following format (there are other
extraneous attributes I've removed for simplicity):

<?xml version="1.0" encoding="UTF-8"?>
<sitenav>
<navitem id="10423" displaytitle="A Child" parentid="" />
<navitem id="10421" displaytitle="A Grandchild" parentid="10423" />
<navitem id="10476" displaytitle="A Great Great Grandchild - parent
is 10434" parentid="10434" />
<navitem id="10424" displaytitle="Another Grandchild"
parentid="10423" />
<navitem id="10434" displaytitle="A Great Grandchild - parent is
10421" parentid="10421" />
<navitem id="10425" displaytitle="Another child" parentid="" />
...
</sitenav>

And need to transform it into this format:

<?xml version="1.0" encoding="UTF-8"?>
<sitenav>
<navitem id="10423" displaytitle="A Child" parentid="">
<navitem id="10421" displaytitle="A Grandchild" parentid="10423">
<navitem id="10434" displaytitle="A Great Grandchild - parent is
10421" parentid="10421">
<navitem id="10476" displaytitle="A Great Great Grandchild - parent
is 10434" parentid="10434" />
</navitem>
</navitem>
<navitem id="10424" displaytitle="Another Grandchild" parentid="10423"
/>
</navitem>
<navitem id="10425" displaytitle="Another child" parentid="" />
...
</sitenav>

Thanks,

Steve

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      12-30-2004
> I have an XML document in the following format (there are other
> extraneous attributes I've removed for simplicity):

Hi,

Nice question...

here's one solution:

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

<xslutput method="xml" indent="yes"/>
<xsl:key name="parentid" match="navitem" use="@parentid"/>

<xsl:template match="sitenav">
<xsl:copy>
<xsl:apply-templates select="navitem[@parentid='']"/>
</xsl:copy>
</xsl:template>

<xsl:template match="navitem">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="key('parentid',@id)"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>


regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
"Scio me nihil scire" - Socrates
 
Reply With Quote
 
 
 
 
Steve Mac
Guest
Posts: n/a
 
      12-31-2004
>Nice question...

>here's one solution:


Thanks Joris!

Would this same approach work to only copy some of the attributes, or
would I need to try a different way?

Steve


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Joris Gillis
Guest
Posts: n/a
 
      12-31-2004
> Would this same approach work to only copy some of the attributes, or
> would I need to try a different way?

The same approach would work.
Just change <xsl:copy-of select="@*"/>
into <xsl:copy-of select="@foo|@bar"/> to only copy the attributes 'foo' and 'bar'

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Gaudiam omnibus traderat W3C , nec vana fides
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
build a hierarchical tree, without using DOM,schema, and sax using expat parser and c pharioum@gmail.com XML 2 11-05-2007 02:24 PM
Using packages in a hierarchical design Shannon VHDL 9 09-11-2007 03:38 PM
Using packages in a hierarchical design Shannon VHDL 0 09-11-2007 04:43 AM
convert flat structure into hierarchical one Ksenia Marasanova Python 3 09-27-2004 02:44 PM
Converting 'flat' gate level names to hierarchical names Paddy McCarthy VHDL 3 09-24-2004 05:34 PM



Advertisments