473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Generating a hierarchical map of nodes in a flat XML file

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
Jun 16 '06 #1
0 1795

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ksenia Marasanova | last post by:
I get this kind of list from a database. (The tuple structure is: id, name, parent_id) I would like to transfer it (in Python) into a tree structure. I don't care much about format, as long...
3
by: Steve Mac | last post by:
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...
3
by: Earl Teigrob | last post by:
I am considering writing a Class that Selects, Adds, Updates and Deletes Nodes in an XML File but do not what to reinvent the wheel. (See XML file below) That data format would emulate records...
1
by: Bruce W.1 | last post by:
I want an XML file to describe hierarchical data where each node is different. Each node will contain different and varied other nodes. Describing this in an XML file would be easy but I'm having...
1
by: jayson_13 | last post by:
Hi, I want to retrieve the hierarchical structure which is store inside the database and then display it using treeview control. But now I got a design issue which I hope that you guys can help...
4
by: Daisy | last post by:
Let's say I've got a forum, where users can be moderators of each forum. Tables look like this: USER -------- user_key name FORUM
5
by: Kent Boogaart | last post by:
Hi, I have some hierarchical data (FAQs) that I would like to bind to. The basic structure is: FAQ Category + Categories + FAQs So an FAQ category has any number of sub-categories and any...
2
by: Don | last post by:
In a previous version of vb I used to save and load hierarchical data from an Access db into a treeview. But, I never found a very satisfying or elegant way to do it. I used a flat file approach...
1
by: doozer1979 | last post by:
Hello, I Have a flat XML file that i have exported from MS access. When i export it from access i want to run it through an xslt to turn it into a hierachical structure Each person has a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.