473,699 Members | 2,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generating a hierarchical map of nodes in a flat XML file

1 New Member
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 1821

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

Similar topics

3
5393
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 as I'll be able to get all the information, based on one id. For example, if I have id=3, I want to get - name ('otherparent')
3
1679
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 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="" />
3
3324
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 in a Database Table with a Primary Key for each Record (see xml sample below) and a flat file structure. I would use a class to manipulate this type of data structure extensivly if I had one. Does anyone know of such an animal?
1
1584
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 a problem with this as related to DTD or XSD. What the XSD really needs to do is describe a node, which can contain other nodes. It could not describe the hierarchy of all the nodes in the XML file. Is it possible to describe a free-form...
1
1797
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 me. My hierarchical structure has five levels which is store in table call level1, level2,… level5. Each level remember the parent ID e.g. level5 contains a column called level4ID. How should I implement it in order to get fastest loading?...
4
2607
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
2664
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 number of FAQs.
2
5862
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 with each item having a reference to its parent node. I would then start by loading the top level nodes folowed by loading child node data only as their parent node was expanded. This method worked fairly well except that allowing the user to...
1
3645
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 "Reports-To" attribute to identify their manager This is what the xml looks like currently:
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.