<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:func="http://exslt.org/functions"
  xmlns:xi="http://www.w3.org/2001/XInclude"
>
  <func:function name="func:document">
    <xsl:param name="url"/>

    <func:result>
      <xsl:apply-templates select="document($url)" mode="xinclude"/>
    </func:result>
  </func:function>


  <xsl:template match="xi:include" mode="xinclude">
    <xsl:copy-of select="func:document(string(@href))"/>
  </xsl:template>

  <xsl:template match="*" mode="xinclude">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="node()" mode="xinclude"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>