Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 19th, 2003, 01:11 AM
Registered User
 
Join Date: Nov 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help in creating folder tree

Hi Friends,
    I need help in creating a folder tree like structure from an xml file. My xml file looks like this.

<topic id = "living part">
  <baseName>
     <baseNameString>
         Living Part
     </baseNameString>
  </baseName>
</topic>
<topic id = "animal">
  <instanceOf>
     <topicRef xlink:href="#living part"/>
  </instanceOf>
  <baseName>
     <baseNameString>
         Animal
     </baseNameString>
  </baseName>
</topic>
<topic id = "carnivorous">
  <instanceOf>
     <topicRef xlink:href="#animal"/>
  </instanceOf>
  <baseName>
     <baseNameString>
         Carnivorous
     </baseNameString>
  </baseName>
</topic>
<topic id = "herbivorous">
  <instanceOf>
     <topicRef xlink:href="#animal"/>
  </instanceOf>
  <baseName>
     <baseNameString>
         Herbivorous
     </baseNameString>
  </baseName>
</topic>
...

My Output should be like,
    Living Part
       Animal
          Carnivorous
          Herbivorous
          ...
       ...

Grateful to any suggestion.

Thanks,
Vel.
 
Old November 19th, 2003, 06:28 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Hi,

I've added DTD to your XML doc (you can make it more "granular"). It's better to define an attribute 'id' as of type ID (and hence you can't use whitespace there):

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE doc[
<!ELEMENT doc (topic)+>
<!ELEMENT topic ANY>
<!ATTLIST topic id ID #REQUIRED>
<!ELEMENT baseNameString ANY>
<!ELEMENT baseName ANY>
<!ELEMENT instanceOf ANY>
<!ELEMENT topicRef ANY>
<!ATTLIST topicRef xlink:href CDATA #REQUIRED>
]>
<doc xmlns:xlink="xlink-URI-here">
<topic id = "livingpart">
  <baseName>
     <baseNameString>
         Living Part
     </baseNameString>
  </baseName>
</topic>
<topic id = "animal">
  <instanceOf>
     <topicRef xlink:href="#livingpart"/>
  </instanceOf>
  <baseName>
     <baseNameString>
         Animal
     </baseNameString>
  </baseName>
</topic>
<topic id = "carnivorous">
  <instanceOf>
     <topicRef xlink:href="#animal"/>
  </instanceOf>
  <baseName>
     <baseNameString>
         Carnivorous
     </baseNameString>
  </baseName>
</topic>
<topic id = "herbivorous">
  <instanceOf>
     <topicRef xlink:href="#animal"/>
  </instanceOf>
  <baseName>
     <baseNameString>
         Herbivorous
     </baseNameString>
  </baseName>
</topic>
</doc>
And the stylesheet is:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="xlink-URI-here">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <xsl:apply-templates select="doc/topic[@id = 'livingpart']"/>
    </xsl:template>
    <xsl:template match="topic">
        <xsl:element name="{@id}">
            <xsl:apply-templates select="/doc/topic[substring-after(instanceOf/topicRef/@xlink:href, '#') = current()/@id]"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>
I've written this quickly, so one can find another more elegant solution.

Regards,
Armen





Similar Threads
Thread Thread Starter Forum Replies Last Post
error creating new folder in asp kingbee Classic ASP Basics 2 March 10th, 2006 05:09 AM
Creating a Tree Structure pazzuzu C++ Programming 2 February 26th, 2005 12:07 PM
Creating Folder On Server Prabhakar_dt Classic ASP Basics 1 December 29th, 2004 05:07 AM
Creating a tree view ravi_talk Classic ASP Basics 0 September 27th, 2003 10:58 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.