Flat to hierarchical using attributes

S

Steve Mac

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
 
J

Joris Gillis

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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput 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,
 
S

Steve Mac

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
 
J

Joris Gillis

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,
 

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

No members online now.

Forum statistics

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

Latest Threads

Top