Subject: Need help in creating folder tree
Posted By: velmj Post Date: 11/19/2003 12:11:22 AM
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.
Reply By: armmarti Reply Date: 11/19/2003 5:28:44 AM
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):


<?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:


<?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

Go to topic 6676

Return to index page 1002
Return to index page 1001
Return to index page 1000
Return to index page 999
Return to index page 998
Return to index page 997
Return to index page 996
Return to index page 995
Return to index page 994
Return to index page 993