<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:dp="http://www.datapower.com/extensions" 
  extension-element-prefixes="dp" 
>
  <xsl:variable name="maxHdrLen" select="127000"/>

  <xsl:template name="CacheSummarizeParts">
    <xsl:param name="target"/>
    <xsl:param name="N" select="1"/>

    <xsl:variable name="response">
      <dp:url-open response="responsecode" 
        target="{concat($target,'.',$N,'.part')}"/>
    </xsl:variable>

    <xsl:if test="$response/url-open/responsecode = 200">
      <xsl:variable name="part" select="$response/url-open/response/*"/>

      <xsl:value-of select="$part"/>

      <xsl:if test="string-length($part)=$maxHdrLen">
        <xsl:call-template name="CacheSummarizeParts">
          <xsl:with-param name="target" select="$target"/>
          <xsl:with-param name="N" select="$N + 1"/>
        </xsl:call-template>      
      </xsl:if>
    </xsl:if>
  </xsl:template>


  <xsl:template match="/">

    <xsl:variable name="base64str" 
      select="dp:http-request-header('X-base64')"/>

    <xsl:variable name="URI" select="dp:variable('var://service/URI')"/>

    <ret>
      <xsl:choose>
        <xsl:when test="string-length($base64str) > 0">
          <xsl:value-of select="$base64str"/>
        </xsl:when>

        <xsl:when test="contains(concat($URI,'&#10;'),'.part&#10;')">
          <xsl:message dp:priority="error" 
            terminate="yes">part entry should have been cached</xsl:message>
        </xsl:when>

        <xsl:otherwise>
          <xsl:call-template name="CacheSummarizeParts">
            <xsl:with-param name="target"  
              select="dp:variable('var://service/URL-in')"/>
          </xsl:call-template>      
        </xsl:otherwise>
      </xsl:choose> 
    </ret>
  </xsl:template>

</xsl:stylesheet>