pguetschow
pguetschow

Reputation: 5337

Remove http(s):// in XSLT 1.0

I'm working on a CMS function where a user could add Meta Data to images. The Option source is used to create a backlink to the original ressource/main page.

This case works finde to determine, a Link should displayed or just the resource.

But I have problems removing the http(s):// from the a href link, as this doesn't look good (the link ist stored in $image_source).

Sadly, XSLT 1.0 doesn't support the replace() method.

Is there any simple workaround for this? Otherwise the option I have would be a bigger template with substring-after().

  <xsl:if test="$image_source!=''">
   <xsl:choose>
   <xsl:when test="starts-with($image_source, 'http://') or starts-with($image_source ,'https://')">
           <span class="image-meta-source">
              <a href="{$image_source}" title="{$image_title}">
                <xsl:value-of select="$image_source"/>
              </a>
            </span>
   </xsl:when>
   <xsl:otherwise>
        <span class="image-meta-source"><xsl:value-of select="$image_source"/></span>
   </xsl:otherwise>
   </xsl:choose>
</xsl:if>

Usually, I'd use RegEx here but afaik this isn't supported (like a lot of useful features) in XSLT 1.0. A regular expression I could think about would be ^(http|https):// Is there a chance to use this, though?

Upvotes: 2

Views: 207

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116993

You could use something like:

<xsl:when test="starts-with($image_source, 'http://') or starts-with($image_source ,'https://')">
    <xsl:value-of select="substring-after($image_source, '://')"/>
</xsl:when>

Upvotes: 3

Related Questions