<?xml version="1.0" encoding="UTF-8"?>

<!-- Note the declaration of the namespace for XInclude. -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
  xmlns:xi="http://www.w3.org/2001/XInclude">

  <!-- =====================================================
    Output to XML
    ===================================================== -->
  <xsl:output method="xml"/>
  
  <!-- =====================================================
    Replace one extension by another
    - linktext: linktext to do substituion in
    - fromextension: Extension to replace (e.g. .html)
    - toextension:Extension to replace it by (e.g. .xml)
    ======================================================-->
  <xsl:template name="replace-extension">
    <xsl:param name="linktext"/>
    <xsl:param name="fromextension"/>
    <xsl:param name="toextension"/>
    <xsl:choose>
      <xsl:when test="contains($linktext, $fromextension)">
        <xsl:value-of select="substring-before($linktext, $fromextension)"/>
        <xsl:value-of select="$toextension"/>
        <xsl:value-of select="substring-after($linktext, $fromextension)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$linktext"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <!-- =====================================================
    By default, simply copy everything 
    ===================================================== -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
  <!-- ===================================================== 
    Special treatment of the include element to prefix the file attribute
    with cocoon:/ and to transform the include element into an
    xi:include. 
    
    For example: 
    
       <include parse="PARSE" file="FILE" pointer="POINTER">
         <xi:fallback>
             .....
         </xi:fallback>
       </include>
    
    is transformed into: 
    
       <xi:include parse="PARSE" file="cocoon:/FILE#POINTER">
         <xi:fallback>
           .....
         </xi:fallback>
       </xi:include>
    
    Adding the cocoon prefix allows us to use URLs relative to the 
    root of the site. Note that the pointer attribute is added to the
    link. This is required since the XInclude processor in 
    Forrest apparently does not support the pointer attribute.
    
    Also, it replaces a .html extension in the file attribute by 
    .xml. 
    ===================================================== -->
  <xsl:template match="include">
    <xsl:element name="xi:include">
      <!-- copy the parse attribute --> 
      <xsl:attribute name="parse">
        <xsl:value-of select="@parse"/>
      </xsl:attribute>
      
      <!-- create the href attribute --> 
      <xsl:attribute name="href">
        <!-- add cocoon prefix --> 
        <xsl:text>cocoon:/</xsl:text>
        
        <!-- replace .html extension by .xml --> 
        <xsl:call-template name="replace-extension">
          <xsl:with-param name="linktext"><xsl:value-of select="@file"/></xsl:with-param>
          <xsl:with-param name="fromextension">.html</xsl:with-param>
          <xsl:with-param name="toextension">.xml</xsl:with-param>
        </xsl:call-template>
        
        <!-- append xpointer to the url --> 
        <xsl:text>#</xsl:text>
        <xsl:value-of select="@pointer"/>
      </xsl:attribute>   
      <xsl:apply-templates/>
    </xsl:element>
   
    
  </xsl:template>
  
  
</xsl:stylesheet>
