<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output 	method="text" 
		encoding="ISO-8859-1" />
<xsl:template match="/">
<xsl:apply-templates select="Ibis_file"/>
</xsl:template>
<xsl:template match="Ibis_file">
<xsl:apply-templates select="comment"/>
<xsl:for-each select="keysection">
<xsl:apply-templates select="."/>
</xsl:for-each>
<xsl:apply-templates select="tablesection"/>
</xsl:template>

<xsl:template match="comment">
<xsl:value-of select="comment_char"/>
<xsl:value-of select="comment_line"/><xsl:text>
</xsl:text></xsl:template>

<xsl:template match="row_comment">
<xsl:value-of select="comment_char"/>
<xsl:value-of select="comment_line"/>
</xsl:template>

<xsl:template match="keysection">
<xsl:apply-templates select="comment"/>
<xsl:apply-templates select="keyword"/>
<xsl:apply-templates select="keydata_section"/>
<xsl:apply-templates select="subkeyword"/>
<xsl:apply-templates select="table"/>
</xsl:template>

<xsl:template match="keyword">
<xsl:value-of select="."/>
<xsl:if test="following-sibling::table" ><xsl:text>
</xsl:text></xsl:if>
</xsl:template>

<xsl:template match="subkeyword">
<xsl:value-of select="."/>
<xsl:if test="following-sibling::subdata" >
<xsl:variable name="fieldlength" select="25"/>
<xsl:variable name="actuallength" select="string-length(.)"/>
<xsl:variable name="padding" select="$fieldlength - $actuallength "/>
<xsl:value-of select="substring('                    ',1,$padding)"/>
<xsl:value-of select="../subdata"/><xsl:text>
</xsl:text></xsl:if>
</xsl:template>


<xsl:template match="keydata_section">
<xsl:for-each select="keydata">
<xsl:if test="position() = 1">
<xsl:variable name="actuallength" select="string-length(../../keyword)"/>
<xsl:variable name="fieldlength" select="20"/>
<xsl:variable name="padding" select="$fieldlength - $actuallength "/>
<xsl:value-of select="substring('                     ',1,$padding)"/>
<xsl:value-of select="."/>
</xsl:if>
<xsl:if test="position() &gt; 1" ><xsl:text>
</xsl:text><xsl:variable name="fieldlength" select="20"/>
<xsl:value-of select="substring('                     ',1,$fieldlength)"/>
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each><xsl:text>
</xsl:text></xsl:template>

<xsl:template match="table">
<xsl:apply-templates select="comment"/>
<xsl:apply-templates select="keyword"/>
<xsl:apply-templates select="keydata"/>
<xsl:apply-templates select="subkeyword"/>
<xsl:apply-templates select="header"/>
<xsl:for-each select="rowdata">
<xsl:apply-templates select="."/><xsl:text>
</xsl:text></xsl:for-each>
</xsl:template>


<xsl:template match="header">
<xsl:for-each select="column">
<xsl:apply-templates select="."/>
</xsl:for-each><xsl:text>
</xsl:text></xsl:template>

<xsl:template match="column">
<xsl:choose>
<xsl:when test="child::keyword">
<xsl:apply-templates select="keyword"/>
<xsl:call-template name ="do-the-padding">
<xsl:with-param name="abc" select="."/>
<xsl:with-param name="width" select="@width"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="child::subkeyword">
<xsl:apply-templates select="subkeyword"/>
<xsl:call-template name ="do-the-padding">
<xsl:with-param name="abc" select="."/>
<xsl:with-param name="width" select="@width"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
<xsl:call-template name ="do-the-padding">
<xsl:with-param name="abc" select="."/>
<xsl:with-param name="width" select="@width"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="rowdata">
<xsl:choose>
<xsl:when test="child::row_comment">
<xsl:apply-templates select="row_comment"/>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="column">
<xsl:choose>
<xsl:when test="child::keyword">
<xsl:apply-templates select="keyword"/>
<xsl:call-template name ="do-the-padding">
<xsl:with-param name="abc" select="."/>
<xsl:with-param name="width" select="@width"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="child::subkeyword">
<xsl:apply-templates select="subkeyword"/>
<xsl:call-template name ="do-the-padding">
<xsl:with-param name="abc" select="."/>
<xsl:with-param name="width" select="@width"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="child::subdata">
<xsl:value-of select="."/>
<xsl:call-template name ="do-the-padding">
<xsl:with-param name="abc" select="."/>
<xsl:with-param name="width" select="@width"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="child::comment">
<xsl:apply-templates select="comment"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="text()"/>
<xsl:call-template name ="do-the-padding">
<xsl:with-param name="abc" select="text()"/>
<xsl:with-param name="width" select="@width"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="do-the-padding">
<xsl:param name="abc" />
<xsl:param name="width" />
<xsl:variable name="pad" select="'                    '"/>
<xsl:variable name="pad-len" select="string-length($pad)"/>
<xsl:variable name="abc_len" select="string-length($abc)"/>
<xsl:variable name="diff" select="$width - $abc_len"/>
<xsl:variable name="effdiff" select="$diff + 3"/>
<xsl:value-of select="substring($pad,1,$effdiff)"/>
</xsl:template>
</xsl:stylesheet>


