<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:lxslt="http://xml.apache.org/xslt"
	xmlns:redirect="org.apache.xalan.xslt.extensions.Redirect"
	extension-element-prefixes="redirect">

<!--
	outlineRedirect.xsl - recursively transform OPML document into HTML
						  and split files by top-level sections
	(c) 2000, Bill Humphries <bill@whump.com>	
-->

<xsl:output method="html" indent="yes"/>

<xsl:variable name="PATH"><xsl:text>/il Potstino/WebSites/whump.com/www/xmlone/</xsl:text></xsl:variable>
<xsl:variable name="MAIN"><xsl:text>index.html</xsl:text></xsl:variable>

<xsl:template match="outlineDocument|opml">
	<redirect:write select="concat($PATH,$MAIN)">
	<html>
		<head>
			<title><xsl:value-of select="head/title"/></title>
			<link rel="stylesheet" type="text/css" href="http://www.whump.com/stylesheets/whump.css"/>	
		</head>
		<body>
			<xsl:apply-templates select="body"/>
		</body>
	</html>
	</redirect:write>
	<p>Writing to <xsl:value-of select="concat($PATH,$MAIN)"/></p>
</xsl:template>

<xsl:template match="body">

	<h1>Outline</h1>
	<ol>
	<xsl:for-each select="outline">
		<li><a href="{concat('section',position(),'.html')}"><xsl:value-of select="@text"/></a></li>
	</xsl:for-each>
	</ol>
	
	<hr />
	
	<xsl:for-each select="outline">
		<redirect:write select="concat($PATH,'section',position(),'.html')">
		<html>
			<head>
				<title><xsl:value-of select="@text"/></title>
				<link rel="stylesheet" type="text/css" href="http://www.whump.com/stylesheets/whump.css"/>	
			</head>
			<body>
				<div class="nav">
					<a href="{$MAIN}">Up</a>
					<xsl:call-template name="prev"/>
					<xsl:call-template name="next"/>
				</div>
				<h1><xsl:value-of select="@text"/></h1>
				<xsl:apply-templates select="outline"/>
			</body>
		</html>
		</redirect:write>
	</xsl:for-each>
	
</xsl:template>

<!--
	At any level in the outline, first test to see if there are any
	children. If there are, write out a header element using the
	current level, and call this template on the children. If
	not, display the node's text in blockquotes.
-->
<xsl:template match="outline">
	<xsl:param name="LEVEL" select="1"/>
	<xsl:choose>
		<xsl:when test="count(outline) &gt; 0">
		<xsl:element name="{concat('h',$LEVEL + 1)}">
			<xsl:call-template name="display"/>
		</xsl:element>
		</xsl:when>
		<xsl:otherwise>
			<blockquote><xsl:call-template name="display"/></blockquote>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:apply-templates select="outline">
		<xsl:with-param name="LEVEL" select="$LEVEL + 1"/>
	</xsl:apply-templates>
</xsl:template>

<!--
	Test to see if the node is a link, if so, render the 
	link. If not, render the plain text.
-->
<xsl:template name="display">
	<xsl:choose>
		<xsl:when test="@type='link'">
			<a href="{@url}"><xsl:value-of select="@text"/></a>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="@text"/>
		</xsl:otherwise>	
	</xsl:choose>
</xsl:template>

<!-- 
	Draw navigation links check the number of the section
	and see if there's a prev or a next : assumes an
	outline node
-->

<xsl:template name="prev">
	<xsl:if test="position() != 1">
		<xsl:text> | </xsl:text><a href="{concat('section', position() - 1,'.html')}">Previous</a>
	</xsl:if>
</xsl:template>

<xsl:template name="next">
	<xsl:if test="position() != last()">
		<xsl:text> | </xsl:text><a href="{concat('section', position() + 1,'.html')}">Next</a>
	</xsl:if>
</xsl:template>

</xsl:stylesheet>