How to add clickable link to XML?

Sunny picture Sunny · Apr 12, 2018 · Viewed 19.8k times · Source

I am trying to make a URL in my XML file clickable. Below is my XML and XSL files which work fine together.

I've tried using XLink and href in XML file but it didn't work.

How do I make my URL in the XML file clickable?

XML

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="file.xsl" ?>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="1.xsl" ?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Johnny</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<url>http://www.yahoo.com</url>
</cd>
</catalog>

XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>XSL</h2>
<table border="1">
<tr bgcolor="#9acd32">
  <th style="text-align:left">Title</th>
  <th style="text-align:left">Artist</th>
  <th style="text-align:left">Country</th>
  <th style="text-align:left">Company</th>
  <th style="text-align:left">Price</th>
  <th style="text-align:left">URL</th>

</tr>
<xsl:for-each select="catalog/cd">
<tr>
  <td><xsl:value-of select="title"/></td>
  <td><xsl:value-of select="artist"/></td>
  <td><xsl:value-of select="country"/></td>
  <td><xsl:value-of select="company"/></td>
  <td><xsl:value-of select="price"/></td>
  <td><xsl:value-of select="url"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

Answer

Tim C picture Tim C · Apr 12, 2018

Before writing any XSLT, you should really know what output you want. And to make a link clickable in HTML you can do this...

<a href="http://www.yahoo.com">http://www.yahoo.com</a>

So, in your XSLT, instead of doing this....

 <td><xsl:value-of select="url"/></td>

Do this...

<td>
    <a href="{url}">
       <xsl:value-of select="url"/>
    </a>
</td>

Note the use of curly braces in the href attribute. This is known as an Attribute Value Template. The curly braces indicate an expression to be evualated, rather than output literally.