trim.common.uri.paths

trim.common.uri.paths — Trim common leading path components from a relative URI

Description

This function trims common leading path components from a relative URI.

<xsl:template name="trim.common.uri.paths">
  <xsl:param name="uriA" select="''"></xsl:param>
  <xsl:param name="uriB" select="''"></xsl:param>
  <xsl:param name="return" select="'A'"></xsl:param>

  <!-- Resolve any ../ in the path -->
  <xsl:variable name="trimmed.uriA">
    <xsl:call-template name="resolve.path">
      <xsl:with-param name="filename" select="$uriA"></xsl:with-param>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="trimmed.uriB">
    <xsl:call-template name="resolve.path">
      <xsl:with-param name="filename" select="$uriB"></xsl:with-param>
    </xsl:call-template>
  </xsl:variable>

  <xsl:choose>
    <xsl:when test="contains($trimmed.uriA, '/') and contains($trimmed.uriB, '/')                     and substring-before($trimmed.uriA, '/') = substring-before($trimmed.uriB, '/')">
      <xsl:call-template name="trim.common.uri.paths">
        <xsl:with-param name="uriA" select="substring-after($trimmed.uriA, '/')"></xsl:with-param>
        <xsl:with-param name="uriB" select="substring-after($trimmed.uriB, '/')"></xsl:with-param>
        <xsl:with-param name="return" select="$return"></xsl:with-param>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:choose>
        <xsl:when test="$return = 'A'">
          <xsl:value-of select="$trimmed.uriA"></xsl:value-of>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$trimmed.uriB"></xsl:value-of>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>