tags:

views:

154

answers:

1

I have some XSL on a page with a URL of:

site.domain.com/dir/dir/dir/dir

The same XSL also appears on a page with URL of:

site.domain.com/dir/dir/dir

I need links from these two pages to point to:

site.domain.com/dir/dir/site

So on both pages I need to get: site.domain.com/dir/dir as part of the HREF attribute.

Can anyone think how I might do this in XSL?

A: 

Step-by-step string processing by recursion:

<xsl:template name="trim-url">
  <xsl:param name="url" select="''" />
  <xsl:param name="lvl" select="0" />

  <xsl:if test="$lvl &gt; 0">
    <xsl:variable name="tmp" select="concat($url, '/')" />

    <xsl:value-of select="substring-before($tmp, '/')" />
    <xsl:value-of select="'/'" />

    <xsl:call-template name="trim-url">
      <xsl:with-param name="url" select="substring-after($tmp, '/')" />
      <xsl:with-param name="lvl" select="$lvl - 1" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

called as follows:

<xsl:variable name="trimmed-url">
  <xsl:call-template name="trim-url">
    <xsl:with-param name="url" select="$url" />
    <xsl:with-param name="lvl" select="3" />
  </xsl:call-template>
  <xsl:value-of select="'site'" />
</xsl:variable>

When $url is 'site.domain.com/dir/dir/dir/dir', then $trimmed-url will be 'site.domain.com/dir/dir/site'.

Tomalak