xsl-list
[Top] [All Lists]

Re: [xsl] Display link text as a hyperlink

2009-03-12 10:01:08
Hey list,

to refresh an old thread again -- how can this be done in XSLT 1.0?
I don't need an exhaustive solution, just one that would cover the
most common URL syntax.

Martynas
www.xml.lt

On Wed, Aug 6, 2008 at 4:00 PM, Michael Kay <mike(_at_)saxonica(_dot_)com> 
wrote:
You seem to be using XSLT 2.0 so this is definitely a case for regular
expressions.

I'm always a bit hesitant about pattern matching in the text - you have to
accept that there will be a proportion of wrong matches and that these will
irritate your users. (Microsoft Powerpoint seems to have a nasty habit of
highlighting XPath expressions and regular expressions as URL hyperlinks,
for example). So I think it's important (a) to be careful about it, and (b)
to be conservative.

A reasonable regex might be something like

\s(http://|www)(\.[a-z0-9])+\.(com|org|net|ca|uk|fr|de|...)[^\s]*

but I'm sure one could improve it.

Michael Kay
http://www.saxonica.com/

-----Original Message-----
From: Brent Solly [mailto:ultra(_at_)ymail(_dot_)com]
Sent: 06 August 2008 15:46
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Display link text as a hyperlink

I have an xml file that contains this character sequence:
'www.gamefaqs.com' .

Primary Problem:
After I convert the 'www' text to hyperlink format, the xsl
displays it as plain text, but I would like to display it has
a hyperlink.

Secondary:
I am aware that the url may also contain subfolders like:
www.gamefaqs.com/console/n64   OR different a suffix like
www.gamefaqs.ca., but right now I'll focus on .com, but feel
free to make suggestions :) .


<?xml version="1.0" encoding="UTF-8"?>
<rss>
   <channel>
      <item>
         <description>More information on this game can be
found at (www.gamefaqs.com)</description>
      </item>
   </channel>
</rss>

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="2.0">
<xsl:strip-space elements="*" />
<xsl:output method="html" encoding="UTF-8"/>


<xsl:template name="globalReplace">
  <xsl:param name="outputString"/>
  <xsl:param name="target"/>
  <xsl:param name="replacement"/>
    <xsl:choose>
   <!--Begin Test -->
   <xsl:when test="contains($outputString,$target)">

      <xsl:value-of select=
        "concat(substring-before($outputString,$target),
               $replacement)"/>
      <xsl:call-template name="globalReplace">
        <xsl:with-param name="outputString"
             select="substring-after($outputString,$target)"/>
        <xsl:with-param name="target" select="$target"/>
        <xsl:with-param name="replacement"
             select="$replacement"/>
      </xsl:call-template>
    </xsl:when>
   <!--End Test-->

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

  </xsl:choose>
</xsl:template>
<xsl:template match="rss">
  <xsl:call-template name="globalReplace">
  <xsl:with-param name="outputString" select="."/>
  <xsl:with-param name="target" select="'www'"/>
  <xsl:with-param name="replacement" select="concat('&lt;a
href=&#34;http://www',substring-before(substring-after(.,'www'
),substring-after(.,'com')),'&#34;&gt;',substring-before(subst
ring-after(.,'www.'),'.com'),'&lt;/a&gt;')"/>

  </xsl:call-template>
</xsl:template>


</xsl:stylesheet>

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>