How to convert url within an XML node to a hyperlink during XSLT transformation?

Hello Everyone,

I need to convert a URL within an XML node into a hyperlink during XSLT transformation.

<xml>
  <mydata>
    this is my data.  my homepage is http://www.mypage.com.  please visit!
  </mydata>
</xml>

How would I convert the URL to a hyperlink?  My URLs always start with "http://".

I am stuck using MSXML 4.0 as my processor.

Thanks in advance for any help provided.
greatseatsAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Geert BormansConnect With a Mentor Information ArchitectCommented:
This is a direct use-case for XSLT2, but I accept that you are bound to msxml4
(please check carefully that you are really bound to msxml4, since there is a .net version of saxon for XSLT2)

Anyway, here is a first stab at it,
If you really need this to be bullet proof, it is a lot of testing etc...
starting the url is easy, ending it is a lot harder
So, how predicatable is your data?
You allready get a good example here: a '.' is allowed in a url, but not as the last character.
It is tough having it as the last character with the space

Can you have multiple http in one text block? If that is teh case, we need recursion.

This can become a very complex issue, so it is important that we allign very good what is expected in the data and what not
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="mydata">
    <xsl:choose>
        <xsl:when test="contains(., 'http://')">
            <xsl:value-of select="substring-before(., 'http://')"/>
            <a href="http://{substring-before(substring-after(., 'http://'), ' ')}">
                <xsl:value-of select="substring-before(substring-after(., 'http://'), ' ')"/>
            </a>
            <xsl:text> </xsl:text>
            <xsl:value-of select="substring-after(substring-after(., 'http://'), ' ')"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="."/>
        </xsl:otherwise>
    </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
greatseatsAuthor Commented:
Gertone,

Thanks - this is a great start.  We only have one instance of "http" per text block.  I will try to implement what you have given me and see how it goes.
0
 
Geert BormansInformation ArchitectCommented:
OK, so no recursion required,
then the only issue will be to find the end of the link
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
greatseatsAuthor Commented:
Gertone,

Your solution worked great.  I made some slight modifications to take into account instances where the URL is at the end of the string and when it is immediately followed by a period, as you mentioned.

Thanks!
0
 
Geert BormansInformation ArchitectCommented:
welcome
0
 
jones1618Commented:
I agree Gertone's solution is a good start. However, AFIK it doesn't handle your original case where some text precedes the URL in the element.

So, you might want to try the following which handles empty elements, elements with no URLs, elements with text before or after the URL, and elements with multiple links.
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="MakingLinks-XSL1.xsl"?>
<xml>
	<mydata descrip="-empty-"      ></mydata>
	<mydata descrip="no link"      >nothing to see here</mydata>
	<mydata descrip="prefix + link">This goes to Google http://google.com</mydata>
	<mydata descrip="link only"    >http://yahoo.com</mydata>
	<mydata descrip="link + suffix">http://msn.com Microsoft</mydata>
	<mydata descrip="two links"    >Tweet http://twitter.com in your face http://facebook.com</mydata>
</xml>
 
<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="xml">
    <html>
    <head>
      <title>Turn URLs into Links</title>
      <style type="text/css">
        body { font-family: Verdana, Times, Serif; }
      </style>
    </head>
    <body>
       <table border="1">
     	<xsl:for-each select="mydata">
        	<TR>
        	<TD align="center"><b><xsl:value-of select="@descrip"/></b></TD>
        	<TD>
			<xsl:call-template name="MakeLinks" >
                    		<xsl:with-param name="text"><xsl:value-of select="text()"/></xsl:with-param>
                    	</xsl:call-template>
        	</TD></TR>
        </xsl:for-each>
     </table>
    </body>
    </html>
  </xsl:template>
  
<xsl:template name="MakeLinks">
	<xsl:param name="text" />
	<xsl:variable name="prefix"       select="substring-before($text, 'http://')" />
	<xsl:variable name="urlremaining" select="substring-after($text, 'http://')" />
	<!-- ==== OUTPUT EVERYTHING UP TO "HTTP" (IF ANY) ============== -->
	<xsl:value-of select="$prefix" />
	<xsl:choose>
	  <!-- ===== NO URL : OUTPUT TEXT ============================== -->
	  <xsl:when test="not($urlremaining)">
		<xsl:value-of select="$text" />
	  </xsl:when>
	  <!-- ===== SPACE AFTER URL: OUTPUT LINK AND PARSE REMAINING == -->
	  <xsl:when test="contains($urlremaining,' ')">
		<xsl:variable name="url" select="substring-before($urlremaining,' ')" />
		<xsl:variable name="remaining" select="substring-after($urlremaining,$url)" />
		<a href="http://{$url}"><xsl:value-of select="$url" /></a>
		<xsl:if test="$remaining">
			<xsl:call-template name="MakeLinks">
			  <xsl:with-param name="text" select="$remaining" />
			</xsl:call-template>
		</xsl:if>
	  </xsl:when>
	  <!-- ===== URL BUT NO SPACE: OUTPUT REST AS LINK ============= -->
	  <xsl:otherwise>
		<xsl:variable name="url" select="$urlremaining" />
		<xsl:variable name="remaining" select="''" />
		<a href="http://{$url}"><xsl:value-of select="$url" /></a>
	  </xsl:otherwise>
	</xsl:choose>
</xsl:template>
 
 </xsl:stylesheet>

Open in new window

0
 
Geert BormansInformation ArchitectCommented:
Hi jones1618, welcome to EE

> However, AFIK it doesn't handle your original case where some text precedes the URL in the element.

Oh yes, it does.
            <xsl:value-of select="substring-before(., 'http://')"/>

The only thing your solution adds is the recursion for multiple urls, which I asked for, it was not a requirement
My stylesheet worked on the original example, a part from the fact that the end-of-sentence "." will be part of the url, which is an error. Your solution has the same problem by the way on the original example, and that is a lot harder to solve genericaly then multiple urls.

If you allow me, I have a little remark on your XSLT
            <xsl:call-template name="MakeLinks" >
                                <xsl:with-param name="text"><xsl:value-of select="text()"/></xsl:with-param>
                          </xsl:call-template>
should better be
            <xsl:call-template name="MakeLinks" >
                                <xsl:with-param name="text"><xsl:value-of select="."/></xsl:with-param>
                          </xsl:call-template>
If there are multiple text nodes (and you can't rely on the fact that there aren't, specially being aware of the cute things msxml does with white-space) text() would only pass the first, potentially an white-space only node... it is a minor detail, but hard to debug

Have fun on EE, we all do

Geert
0
 
jones1618Commented:
Greatseats,

Handling trailing periods shouldn't be too hard. After getting the URL, just add an xsl:choose clause that truncates the URL with substring if it ends-with '.'  and appends a '.' after the link. Otherwise output the whole URL as now.

Gertone,

Thanks for your suggestion about using "." instead of "text()". Good catch.

Clearly your XSL skills are deeper than mine, but in this case, when I tested your quick solution it fell down for two test cases: 1) where mydata contains text preceding a URL and 2) where mydata contains nothing but a URL. In general, your XSL only works if there's a space after the URL.

Try the attached XSL which wraps your original xsl:choose statement in an HTML table to show the results. Just apply it to the six XML test cases attached to my previous comment. You'll see that It fails for two test cases. My XSL handles those cases (along with multiple links which wasn't strictly required, as you said).
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="xml">
    <html>
    <head>
      <title>Turn URLs into Links</title>
      <style type="text/css">
        body { font-family: Verdana, Times, Serif; }
      </style>
    </head>
    <body>
       <table border="1">
     	<xsl:for-each select="mydata">
        	<TR>
        	<TD align="center"><b><xsl:value-of select="@descrip"/></b></TD>
        	<TD>
<!-- ==== Gerton's original code ================================ -->
		    <xsl:choose>
			<xsl:when test="contains(., 'http://')">
			    <xsl:value-of select="substring-before(., 'http://')"/>
			    <a href="http://{substring-before(substring-after(., 'http://'), ' ')}">
				<xsl:value-of select="substring-before(substring-after(., 'http://'), ' ')"/>
			    </a>
			    <xsl:text> </xsl:text>
			    <xsl:value-of select="substring-after(substring-after(., 'http://'), ' ')"/>
			</xsl:when>
			<xsl:otherwise>
			    <xsl:value-of select="."/>
			</xsl:otherwise>
		    </xsl:choose>
<!-- ============================================================ -->
        	</TD></TR>
        </xsl:for-each>
     </table>
    </body>
    </html>
 </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
Geert BormansInformation ArchitectCommented:
yep, I know it has flaws, think I indicated that with my proposal, I appreciate your suggestions though

you are right that stripping the . is easy, but there is also : ; , ! ? etc.
developing a full-blown true parser for urls is difficult and not a straightforward task in XSLT1

The good thing about this discussion is that it shows to future readers of this thread that what I posted as a
"here is a first stab at it" is NOT a definite answer to the generic question

thanks
0
 
jones1618Commented:
Attached is my final take on the more general problem. Features:

1. URLs can appear anywhere in the element (first, last or middle)
2. Elements may contain multiple URLs
3. Trailing .,?! characters are not included in URLs. So Google.com! becomes <Google.com>!
4. Elements can include other HTML.elements (In Internet Explorer not Firefox, unfortunately.)
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="MakingLinks-XSL1.xsl"?>
<xml>
	<mydata descrip="-empty-"      ></mydata>
	<mydata descrip="no link"      >nothing to see here</mydata>
	<mydata descrip="prefix + link">All my search are belong to http://google.com</mydata>
	<mydata descrip="link only"    >http://yahoo.com</mydata>
	<mydata descrip="link + suffix">http://msn.com? That's a Microsoft thing.</mydata>
	<mydata descrip="link + comma"><![CDATA[The <b>best</b> site is http://newsmap.jp, isn't it?]]></mydata>
	<mydata descrip="two links"    ><![CDATA[Sweet tweet http://twitter.com. <br />In your <b>face</b> http://facebook.com!]]></mydata>
</xml>
 
<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
 
<xsl:template match="*">
    <xsl:apply-templates/>
</xsl:template>
 
  <xsl:template match="xml">
    <html>
    <head>
      <title>Turn URLs into Links</title>
      <style type="text/css">
        body { font-family: Verdana, Times, Serif; }
      </style>
    </head>
    <body>
       <table border="1">
     	<xsl:for-each select="mydata">
        	<TR>
        	<TD align="center"><b><xsl:value-of select="@descrip"/></b></TD>
        	<TD>
			<xsl:call-template name="ReplaceLinks" >
                    		<xsl:with-param name="text"><xsl:value-of select="."/></xsl:with-param>
                    	</xsl:call-template>
        	</TD></TR>
        </xsl:for-each>
     </table>
    </body>
    </html>
  </xsl:template>
  
<xsl:template name="ReplaceLinks">
	<xsl:param name="text" />
	<xsl:variable name="prefix"       select="substring-before($text, 'http://')" />
	<xsl:variable name="urlremaining" select="substring-after($text, 'http://')" />
	<!-- ==== OUTPUT EVERYTHING UP TO "HTTP" (IF ANY) ============== -->
	<xsl:value-of select="$prefix" disable-output-escaping="yes"/>
	<xsl:choose>
	  <!-- ===== NO URL : OUTPUT TEXT ============================== -->
	  <xsl:when test="not($urlremaining)">
		<xsl:value-of select="$text" disable-output-escaping="yes"/>
	  </xsl:when>
	  <!-- ===== SPACE AFTER URL: OUTPUT LINK AND PARSE REMAINING == -->
	  <xsl:when test="contains($urlremaining,' ')">
		<xsl:variable name="url" select="substring-before($urlremaining,' ')" />
		<xsl:variable name="remaining" select="substring-after($urlremaining,$url)" />
		<xsl:call-template name="WriteLink" >
			<xsl:with-param name="url"><xsl:value-of select="$url"/></xsl:with-param>
		</xsl:call-template>
		<xsl:if test="$remaining">
			<xsl:call-template name="ReplaceLinks">
			  <xsl:with-param name="text" select="$remaining" />
			</xsl:call-template>
		</xsl:if>
	  </xsl:when>
	  <!-- ===== URL BUT NO SPACE: OUTPUT REST AS LINK ============= -->
	  <xsl:otherwise>
		<xsl:variable name="url" select="$urlremaining" />
		<xsl:variable name="remaining" select="''" />
		<xsl:call-template name="WriteLink" >
			<xsl:with-param name="url"><xsl:value-of select="$url"/></xsl:with-param>
		</xsl:call-template>
	  </xsl:otherwise>
	</xsl:choose>
</xsl:template>
 
<xsl:template name="WriteLink">
	<xsl:param name="url" />
	<xsl:variable name="lastc" select="substring($url,string-length($url),1)" />
	<xsl:choose>
		<xsl:when test="$lastc='.' or $lastc=',' or $lastc='?' or $lastc='!'">
			<a target="_blank" href="http://{substring($url,1,string-length($url)-1)}"><xsl:value-of select="substring($url,1,string-length($url)-1)" /></a><xsl:value-of select="$lastc" />
		</xsl:when>
		<xsl:otherwise>
			<a target="_blank" href="http://{$url}"><xsl:value-of select="$url" /></a>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>
 
</xsl:stylesheet>

Open in new window

0
All Courses

From novice to tech pro — start learning today.