<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="ISO-8859-1"/>


<!-- ###################################################################### -->
<!-- ### Document                                                           -->
<!-- ###################################################################### -->
<xsl:template match="/">

  <!-- Display this node -->
  <ZMSRubrik>
    <active><lang>1</lang></active>
    <title><lang>HTML-Import</lang></title>
    <titleshort><lang>HTML-Import</lang></titleshort>
    
    <!-- Process child-nodes -->
    <xsl:for-each select="//h1">
      <xsl:apply-templates select="."/>
    </xsl:for-each>
    
  </ZMSRubrik>
  
</xsl:template>



<!-- ###################################################################### -->
<!-- ### Heading 1                                                          -->
<!-- ###################################################################### -->
<xsl:template match="h1">

  <!-- Level of this node (passed as parameter!) -->
  <xsl:param name="level" select="1"/>
  
  <!-- Name of this node. -->
  <xsl:variable name="seq_name" select="name()"/>
  
  <!-- Maximum for elements is index of this node's following-sibling with the same name. -->
  <xsl:variable name="seq_max">
    <xsl:choose>
      <xsl:when test="count(following-sibling::*[name()=$seq_name])=0">
        0
      </xsl:when>
      <xsl:otherwise>
        <xsl:for-each select="following-sibling::*[name()=$seq_name][1]">
          <xsl:number level="single" count="*" format="1"/>
        </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable> 
  
  <!-- Display this node -->
  <ZMSDocument>
    <active><lang>1</lang></active>
    <title><lang><xsl:apply-templates/></lang></title>
    <titleshort><lang><xsl:apply-templates/></lang></titleshort>
      
    <!-- Process child-nodes -->
    <xsl:for-each select="following-sibling::*">
      <xsl:variable name="seq_idx"><xsl:number level="single" count="*" format="1"/></xsl:variable>
      <xsl:if test="$seq_max = 0 or $seq_idx &lt; $seq_max">
        <xsl:apply-templates select="." />
      </xsl:if>
    </xsl:for-each>

  </ZMSDocument>

</xsl:template>



<!-- ###################################################################### -->
<!-- ### Headings 2..6                                                      -->
<!-- ###################################################################### -->
<xsl:template match="h2|h3|h4|h5|h6">

  <!-- Format -->
  <xsl:variable name="format">
    <xsl:choose>
      <xsl:when test="name()='h2'">headline_2</xsl:when>
      <xsl:when test="name()='h3'">headline_3</xsl:when>
      <xsl:when test="name()='h4'">headline_4</xsl:when>
      <xsl:when test="name()='h5'">headline_5</xsl:when>
      <xsl:when test="name()='h6'">headline_6</xsl:when>
    </xsl:choose>
  </xsl:variable> 

  <!-- Text -->
  <xsl:variable name="text">
    <xsl:apply-templates/>
  </xsl:variable> 

  <!-- Display this node -->
  <xsl:if test="string-length($text)>0">
    <ZMSTextarea>
      <active><lang>1</lang></active>
      <format><xsl:value-of select="$format"/></format>
      <text><lang><xsl:apply-templates/></lang></text>
    </ZMSTextarea>
  </xsl:if>

</xsl:template>



<!-- ###################################################################### -->
<!-- ### Paragraphs                                                         -->
<!-- ###################################################################### -->
<xsl:template match="p">

  <xsl:choose>

    <!-- Process images -->  
    <xsl:when test="count(descendant::img)>0">
      <xsl:apply-templates/>
    </xsl:when>
    
    <!-- Process paragraph -->
    <xsl:otherwise>
    
      <!-- Format -->
      <xsl:variable name="format">
        <xsl:choose>
          <xsl:when test="@class='MsoCaption'">caption</xsl:when>
          <xsl:when test="@class='Merksatz' or @class='Emphasis'">emphasis</xsl:when>
          <xsl:otherwise>body</xsl:otherwise>
        </xsl:choose>
      </xsl:variable> 

      <!-- Text -->
      <xsl:variable name="text">
        <xsl:apply-templates/>
      </xsl:variable> 

      <!-- Display this node -->
      <xsl:if test="string-length($text)>0">
        <ZMSTextarea>
          <active><lang>1</lang></active>
          <format><xsl:value-of select="$format"/></format>
          <text><lang><xsl:apply-templates/></lang></text>
        </ZMSTextarea>
      </xsl:if>
      
    </xsl:otherwise>

  </xsl:choose>
  
</xsl:template>



<!-- ###################################################################### -->
<!-- ### Tables                                                             -->
<!-- ###################################################################### -->

  <!-- ===== -->
  <!-- Table -->
  <!-- ===== -->
  <xsl:template match="table">
    <ZMSTextarea>
      <active><lang>1</lang></active>
      <format>html</format>
      <text>
        <lang>
          <table>
            <xsl:attribute name="border">1</xsl:attribute>
            <xsl:apply-templates/>
          </table>
        </lang>
      </text>
    </ZMSTextarea>
  </xsl:template>

  <!-- ========= -->
  <!-- Table.Row -->
  <!-- ========= -->
  <xsl:template match="table/tr">
    <tr><xsl:apply-templates/></tr>
  </xsl:template>

  <!-- ============ -->
  <!-- Table.Header -->
  <!-- ============ -->
  <xsl:template match="table/tr/th">
    <th>
      <xsl:if test="string-length(@colspan)>0"><xsl:attribute name="colspan"><xsl:value-of select="@colspan" /></xsl:attribute></xsl:if>
      <xsl:if test="string-length(@rowspan)>0"><xsl:attribute name="colspan"><xsl:value-of select="@rowspan" /></xsl:attribute></xsl:if>
      <xsl:apply-templates/>
    </th>
  </xsl:template>
  <xsl:template match="table/tr/th/p">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- ========== -->
  <!-- Table.Cell -->
  <!-- ========== -->
  <xsl:template match="table/tr/td">
    <td>
      <xsl:if test="string-length(@colspan)>0"><xsl:attribute name="colspan"><xsl:value-of select="@colspan" /></xsl:attribute></xsl:if>
      <xsl:if test="string-length(@rowspan)>0"><xsl:attribute name="colspan"><xsl:value-of select="@rowspan" /></xsl:attribute></xsl:if>
      <xsl:apply-templates/>
    </td>
  </xsl:template>
  <xsl:template match="table/tr/td/p">
    <xsl:apply-templates/>
  </xsl:template>


<!-- ###################################################################### -->
<!-- ### Lists                                                              -->
<!-- ###################################################################### -->

  <!-- ============== -->
  <!-- Unordered List -->
  <!-- ============== -->
  <xsl:template match="ul">
    <ZMSTextarea>
      <active><lang>1</lang></active>
      <format>unordered_list</format>
      <text><lang><xsl:apply-templates/></lang></text>
    </ZMSTextarea>
  </xsl:template>

  <!-- ============ -->
  <!-- Ordered List -->
  <!-- ============ -->
  <xsl:template match="ol">
    <ZMSTextarea>
      <active><lang>1</lang></active>
      <format>ordered_list</format>
      <text><lang><xsl:apply-templates/></lang></text>
    </ZMSTextarea>
  </xsl:template>

  <!-- ========= -->
  <!-- List Item -->
  <!-- ========= -->
  <xsl:template match="li|li/p">
    <xsl:apply-templates/>
  </xsl:template>




<!-- ###################################################################### -->
<!-- ### Images                                                         -->
<!-- ###################################################################### -->
<xsl:template match="img">
  <ZMSGraphic>
    <active><lang>1</lang></active>
    <img><lang><data>
      <xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
      <xsl:attribute name="height"><xsl:value-of select="@width"/></xsl:attribute>
      <xsl:attribute name="filename"><xsl:value-of select="@src"/></xsl:attribute>
    </data></lang></img>
  </ZMSGraphic>
</xsl:template>



<!-- ###################################################################### -->
<!-- ### Character-Formats (Bold, Italic, Underline)                        -->
<!-- ###################################################################### -->
<xsl:template match="b">
  <b><xsl:apply-templates/></b>
</xsl:template>
<xsl:template match="i">
  <i><xsl:apply-templates/></i>
</xsl:template>
<xsl:template match="u">
  <u><xsl:apply-templates/></u>
</xsl:template>



<!-- ###################################################################### -->
<!-- ### Hyperlinks                                                         -->
<!-- ###################################################################### -->
<xsl:template match="a">
  <a>
    <xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>
    <xsl:attribute name="target">_blank</xsl:attribute>
    <xsl:apply-templates/>
  </a>
</xsl:template>



<!-- ###################################################################### -->
<!-- ### Text Nodes                                                         -->
<!-- ###################################################################### -->
<xsl:template match="h1/text()|h2/text()|h3/text()|h4/text()|h5/text()|h6/text()|p/text()|b/text()|i/text()|u/text()">
  <xsl:call-template name="output-nl">
     <xsl:with-param name="text" select="." />
  </xsl:call-template>
</xsl:template>



<!-- ###################################################################### -->
<!-- ### Unhandled Tags                                                     -->
<!-- ###################################################################### -->
<xsl:template match="html|body|div|span">
  <xsl:apply-templates/>
</xsl:template>



<!-- ###################################################################### -->
<!-- ### Unprocessed Tags                                                     -->
<!-- ###################################################################### -->
<xsl:template match="style|meta|title" />



<!-- ###################################################################### -->
<!-- ### Procedures / Functions                                             -->
<!-- ###################################################################### -->

  <!-- ========================================== -->
  <!-- replace in $value substring $from with $to -->
  <!-- ========================================== -->
  <xsl:template name="replace-substring">
    <xsl:param name="value" />
    <xsl:param name="from" />
    <xsl:param name="to" />
    <xsl:choose>
         <xsl:when test="contains($value,$from)">
            <xsl:value-of select="substring-before($value,$from)" />
            <xsl:value-of select="$to" />
            <xsl:call-template name="replace-substring">
               <xsl:with-param name="value" select="substring-after($value,$from)" />
               <xsl:with-param name="from" select="$from" />
               <xsl:with-param name="to" select="$to" />
            </xsl:call-template>
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="$value" />
         </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- ========= -->
  <!-- output-nl -->
  <!-- ========= -->
  <xsl:template name="output-nl">
    <xsl:param name="text" />

    <xsl:variable name="tmp">
      <xsl:call-template name="replace-substring">
        <xsl:with-param name="from" select="'&#xA;'" />
        <xsl:with-param name="to" select="' '" />
        <xsl:with-param name="value">
          <xsl:call-template name="replace-substring">
            <xsl:with-param name="from" select="'&#160;'" />
            <xsl:with-param name="to" select="''" />
            <xsl:with-param name="value" select="$text" />
          </xsl:call-template>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:variable>

    <xsl:value-of select="$tmp" />
  </xsl:template>
      
<!-- =========================================================== -->
      
</xsl:stylesheet>

