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:
- <NoName>
-
<topic id="id1" role="1">
-
<title>Heading 1 Title</title>
-
<body>
-
<p>Some text.</p>
-
</body>
-
</topic>
-
<topic id="id2" role="2">
-
<title>Heading 2 Title</title>
-
<body>
-
<p>Some text.</p>
-
</body>
-
</topic>
-
<topic id="id3" role="2">
-
<title>Heading 2 Title</title>
-
<body>
-
<p>Some text.</p>
-
</body>
-
</topic>
-
<topic id="id4" role="3">
-
<title>Heading 2 Title</title>
-
<body>
-
<p>Some text.</p>
-
</body>
-
</topic>
-
<topic id="id1" role="1">
-
<title>Heading 1 Title</title>
-
<body>
-
<p>Some text.</p>
-
</body>
-
</topic>
-
<topic id="id2" role="2">
-
<title>Heading 2 Title</title>
-
<body>
-
<p>Some text.</p>
-
</body>
-
</topic>
-
</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).
- <map>
-
<topic href="id1.xml" navtitle="Heading 1" type="topic">
-
<topic href="id2.xml" navtitle="Heading 2" type="topic"></topic>
-
<topic href="id3.xml" navtitle="Heading 2" type="topic">
-
<topic href="id4.xml" navtitle="Heading 3" type="topic"></topic>
-
</topic>
-
<topic href="id1.xml" navtitle="Heading 1" type="topic">
-
<topic href="id2.xml" navtitle="Heading 2" type="topic"></topic>
-
</topic>
-
</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.
- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
-
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
-
<xsl:template match="NoName">
-
<main>
-
<xsl:apply-templates select="topic[@role='1']" />
-
</main>
-
</xsl:template>
-
-
<xsl:template name="hierarchy" match="/NoName">
-
<xsl:variable name="id" select="@Id"/>
-
<xsl:variable name="title" select="title"/>
-
<xsl:for-each select="topic[@role='1']">
-
<xsl:sort select="."/>
-
<map title="{$title}" id="{$id}.xml">
-
<xsl:for-each select="topic[@role='2']">
-
<xsl:sort select="."/>
-
<topicref href="{$id}.xml" navtitle="{$title}" type="topic">
-
<xsl:for-each select="topic[@role='3']">
-
<xsl:sort select="."/>
-
<topicref href="{$id}.xml" navtitle="{$title}" type="topic">
-
<xsl:for-each select="topic[@role='4']">
-
<xsl:sort select="."/>
-
<topicref href="{$id}.xml" navtitle="{$title}" type="topic"/>
-
</xsl:for-each>
-
</topicref>
-
</xsl:for-each>
-
</topicref>
-
</xsl:for-each>
-
</map>
-
</xsl:for-each>
-
</xsl:template>
-
</xsl:stylesheet>
If anyone could point me in the right direction, I'd sure appreciate it!
Thanks!
Mark Peters