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
- <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>
Expand|Select|Wrap|Line Numbers
- <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>
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
- <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>
Thanks!
Mark Peters