Connecting Tech Pros Worldwide Help | Site Map

Generating a hierarchical map of nodes in a flat XML file

Newbie
 
Join Date: Jun 2006
Location: Baltimore, MD
Posts: 1
#1: Jun 16 '06
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 in the hierarchy (role="1" being the top node).

Here is the original XML file structure:

Expand|Select|Wrap|Line Numbers
  1. <NoName>
  2.     <topic id="id1" role="1">
  3.         <title>Heading 1 Title</title>
  4.         <body>
  5.             <p>Some text.</p>
  6.         </body>
  7.     </topic>
  8.     <topic id="id2" role="2">
  9.         <title>Heading 2 Title</title>
  10.         <body>
  11.             <p>Some text.</p>
  12.         </body>
  13.     </topic>
  14.     <topic id="id3" role="2">
  15.         <title>Heading 2 Title</title>
  16.         <body>
  17.             <p>Some text.</p>
  18.         </body>
  19.     </topic>
  20.     <topic id="id4" role="3">
  21.         <title>Heading 2 Title</title>
  22.         <body>
  23.             <p>Some text.</p>
  24.         </body>
  25.     </topic>
  26.     <topic id="id1" role="1">
  27.         <title>Heading 1 Title</title>
  28.         <body>
  29.             <p>Some text.</p>
  30.         </body>
  31.     </topic>
  32.     <topic id="id2" role="2">
  33.         <title>Heading 2 Title</title>
  34.         <body>
  35.             <p>Some text.</p>
  36.         </body>
  37.     </topic>
  38. </NoName>
My goal is to generate a hierarchical map of the topics, include the topic "id" attribute and title value within the topic node, and discard the other subnodes (body, p).

Expand|Select|Wrap|Line Numbers
  1. <map>
  2.     <topic  href="id1.xml" navtitle="Heading 1" type="topic">
  3.           <topic  href="id2.xml"  navtitle="Heading 2" type="topic"></topic>
  4.           <topic  href="id3.xml"  navtitle="Heading 2" type="topic">
  5.                  <topic  href="id4.xml" navtitle="Heading 3"  type="topic"></topic>
  6.       </topic>
  7.     <topic  href="id1.xml" navtitle="Heading 1" type="topic">
  8.           <topic  href="id2.xml"  navtitle="Heading 2" type="topic"></topic>
  9.       </topic>
  10. </map>
After mapping the various topics in my XML file, I'll then be able to chunk the topics into individual XML files and reference them using the map. The map architecture is based on an existing DTD.

I thought I could get by using variables and for-each statements, but my code (below) generates nothing (no errors). I've also played around with keys, but have produced nothing but errors in Xalan so far.

Expand|Select|Wrap|Line Numbers
  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2.     <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  3. <xsl:template match="NoName">
  4.  <main>
  5.  <xsl:apply-templates select="topic[@role='1']" />
  6.  </main>
  7.  </xsl:template>
  8.  
  9.     <xsl:template name="hierarchy" match="/NoName">
  10.     <xsl:variable name="id" select="@Id"/>
  11.     <xsl:variable name="title" select="title"/>
  12.         <xsl:for-each select="topic[@role='1']">
  13.             <xsl:sort select="."/>
  14.             <map title="{$title}" id="{$id}.xml">
  15.                 <xsl:for-each select="topic[@role='2']">
  16.                     <xsl:sort select="."/>
  17.                     <topicref href="{$id}.xml" navtitle="{$title}" type="topic">
  18.                         <xsl:for-each select="topic[@role='3']">
  19.                             <xsl:sort select="."/>
  20.                             <topicref href="{$id}.xml" navtitle="{$title}" type="topic">
  21.                                 <xsl:for-each select="topic[@role='4']">
  22.                                     <xsl:sort select="."/>
  23.                                     <topicref href="{$id}.xml" navtitle="{$title}" type="topic"/>
  24.                                 </xsl:for-each>
  25.                             </topicref>
  26.                         </xsl:for-each>
  27.                     </topicref>
  28.                 </xsl:for-each>
  29.             </map>
  30.         </xsl:for-each>
  31.     </xsl:template>
  32. </xsl:stylesheet>
If anyone could point me in the right direction, I'd sure appreciate it!


Thanks!

Mark Peters
Reply