Re: [xsl] Identifing link (advanced)

Subject: Re: [xsl] Identifing link (advanced)
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 7 Jun 2002 18:42:48 +0100
Hi Raimund,

> What I wanted to do is to also identify the 'file:/' and 'file:///'
> URL's. This sounds like an easy choose ... clause - but when I went
> into the detail I got again stuck.

Well, if the string contains 'file:///' then it must contain 'file:/'
(since 'file:///' starts with 'file:/') so actually you need to test
for two possibilities: 'http://' and 'file:/'.

You can't know whether the string will contain a 'file:/' or a
'http://' first, so you have to go through the string twice, once to
check for 'file:/' and once to check for 'http://'. The best way of
doing this is to have a doubly-recursive template -- one that goes on
to process the string *before* the link as well as the string *after*
the link, at least if there's a possibility of another link being
there:

<xsl:template name="add-link">
  <xsl:param name="string" />
  <xsl:choose>
    <xsl:when test="contains($string, 'http://')">
      <!-- add links to the string *before* the 'http://' -->
      <xsl:call-template name="add-link">
        <xsl:with-param name="string"
          select="substring-before($string, 'http://')" />
      </xsl:call-template>

      <xsl:variable name="rest"
                    select="substring-after($string, 'http://')" />
      <xsl:variable name="URL"
        select="concat('http://', substring-before($rest, ' '))">

      <!-- create the link -->
      <a href="{$URL}"><xsl:value-of select="$URL" /></a>

      <!-- process the string after the link -->
      <xsl:call-template name="add-link">
        <xsl:with-param name="string"
                        select="substring-after($rest, ' ')" />
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="contains($string, 'file:/')">
      <!-- give the value of the string before the 'file:/' -
           it can't contain 'http://' or 'file:/' -->
      <xsl:value-of select="substring-before($string, 'file:/')" />

      <xsl:variable name="rest"
                    select="substring-after($string, 'file:/')" />
      <xsl:variable name="URL"
        select="concat('file:/', substring-before($rest, ' '))">

      <!-- create the link -->
      <a href="{$URL}"><xsl:value-of select="$URL" /></a>

      <!-- process the string after the link -->
      <xsl:call-template name="add-link">
        <xsl:with-param name="string"
                        select="substring-after($rest, ' ')" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$string" />
    </xsl:otherwise>
  </xsl:choose>

If you keep adding URL types, you might find it helpful to store those
types in an XML structure and refer to them that way rather than
adding more and more xsl:when clauses, or to use one of the
off-the-shelf search and replace implementations, such as Dimitre's at
http://www.vbxml.com/downloads/default.asp?id=v2001611171627 or the
EXSLT one from http://www.exslt.org/str/functions/replace/. If you do
that and you need help, don't hesitate to post again.
  
Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread