<?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">
  
  <!-- =====================================================
    The directory path of the URL. This will be prefixed where
    needed to obtain a path relative to the root of the site
    ======================================================== -->
  <xsl:param name="path" select="a/b"/>
  
  <!-- =====================================================
    Output to XML
    ===================================================== -->
  <xsl:output method="xml"/>
  
  <!-- =====================================================
    Special treatment of the root element, generate a DTD declaration for the document 
    ===================================================== -->
  <xsl:template match="document">
    
    <!-- Forrest does not like it when we put a DTD declaration in the document --> 
    <!--   <xsl:text>&#10;</xsl:text>
      <xsl:text disable-output-escaping="yes">&lt;!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd"&gt;</xsl:text>
      <xsl:text>&#10;</xsl:text>
    -->
    
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </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 transform it 
    into an Xinclude that includes the 
    content of the body tag in the referred XML file.
    
    For example, when the value of the path parameter is a/b,
    
    <include parse="PARSE" file="FILE" pointer="POINTER"/>
    
    is transformed into
    
    <include parse="PARSE" file="a/b/FILE" pointer="POINTER">
    <xi:fallback>
    <p> Could not find include FILE</p>
    </xi:fallback>
    </include>
    
    In other words, it transforms the url into a url relative
    to the root of the site. Note that the fallback element is 
    already in the XInclude namespace whereas the include element 
    is not. This is required since the link rewriter apparently
    only does link rewriting for the default namespace (next step).
    Therefore we must keep the include element in the default
    namespace. 
    
    The transformation of the file attribute is not done in case the 
    FILE contains a semicolon, in which case the script assumes 
    that we are dealing either with some scheme in Forrest such
    as site: or ext: 
    ===================================================== -->
  <xsl:template match="include">
    <xsl:variable name="link"><xsl:value-of select="@file"/></xsl:variable>
    <xsl:variable name="fulllink">
      <xsl:if test="not(contains($link, ':'))">
        <!-- This is not a site or ext link so we need to put the given path in front of it -->
        <xsl:if test="not(starts-with($link, '/'))">
          <!-- This is a relative link --> 
          <xsl:value-of select="$path"/>
          <xsl:text>/</xsl:text>
        </xsl:if>
      </xsl:if>   
      <xsl:value-of select="$link"/>
    </xsl:variable>
    
    <xsl:element name="includedcontent">
      <xsl:attribute name="file">
        <xsl:value-of select="$fulllink"/>
      </xsl:attribute>
      <xsl:element name="include" inherit-namespaces="no">
        <!-- copy the parse attribute --> 
        <xsl:attribute name="parse">
          <xsl:value-of select="@parse"/>
        </xsl:attribute>
        
        <!-- modify the file attribute if necessary --> 
        <xsl:attribute name="file">   
          <xsl:value-of select="$fulllink"/>
          <!-- xsl:if test="not(contains($link, ':'))">
            <xsl:if test="not(starts-with($link, '/'))">
            <xsl:value-of select="$path"/>
            <xsl:text>/</xsl:text>
            </xsl:if>
            </xsl:if>   
            <xsl:value-of select="$link"/ -->
        </xsl:attribute>
        
        <!-- copy the pointer attribute --> 
        <xsl:attribute name="pointer">
          <xsl:value-of select="@pointer"/>
        </xsl:attribute>
        
        <!-- create a fallback element --> 
        <xsl:element name="xi:fallback"  inherit-namespaces="no">
          <p>Could not find include &apos;<xsl:value-of select="@file"/>&apos;</p>
        </xsl:element>
      </xsl:element>
    </xsl:element>
  </xsl:template>
  
</xsl:stylesheet>
